Merging changes from master into my branch
Categories:
Mastering Git Merges: Bringing Changes into Your Branch

Learn how to effectively merge changes from the master (or main) branch into your feature branch using Git, resolving conflicts, and maintaining a clean history.
Working with Git often involves creating feature branches to develop new functionalities or fix bugs in isolation. While you're working on your branch, the master
(or main
) branch might be updated with new commits from other team members. To ensure your feature branch stays up-to-date and to avoid large, complex merge conflicts later, it's crucial to regularly merge changes from master
into your branch. This article will guide you through the process, explaining the commands and best practices.
Why Merge master
into Your Feature Branch?
Regularly merging master
into your feature branch is a fundamental practice in collaborative Git workflows. It offers several key benefits:
- Conflict Prevention: By integrating changes frequently, you encounter smaller, more manageable conflicts, rather than a massive conflict resolution task when you're ready to merge your feature back into
master
. - Up-to-date Context: Your feature branch always has the latest code from
master
, ensuring you're developing against the most current version of the project. This prevents you from building features based on outdated assumptions. - Early Detection of Issues: Integrating
master
changes can sometimes reveal unexpected interactions or bugs between your work and others' work earlier in the development cycle, making them easier to fix. - Smoother Final Merge: When your feature is complete, merging your branch back into
master
will be much simpler, as most integration work has already been done incrementally.
flowchart TD A[Start Feature Branch] --> B{Develop Feature} B --> C{Master Updated?} C -- Yes --> D[Fetch & Checkout Master] D --> E[Pull Latest Master] E --> F[Checkout Feature Branch] F --> G[Merge Master into Feature] G --> H{Resolve Conflicts?} H -- Yes --> I[Commit Merge] H -- No --> I[Commit Merge] I --> B C -- No --> B
Workflow for merging master into a feature branch
The Merging Process: Step-by-Step
Merging changes from master
into your feature branch involves a few straightforward Git commands. It's important to follow these steps carefully to ensure a clean merge.
1. Step 1: Ensure Your Working Directory is Clean
Before switching branches, make sure you don't have any uncommitted changes in your current feature branch. You can either commit them or stash them. This prevents losing work or carrying uncommitted changes to another branch.
2. Step 2: Switch to Your master
(or main
) Branch
You need to be on the master
branch to pull the latest changes from the remote repository. Use git checkout master
.
3. Step 3: Pull the Latest Changes from Remote master
Update your local master
branch with the most recent commits from the remote. This ensures you're merging the absolute latest version. Use git pull origin master
.
4. Step 4: Switch Back to Your Feature Branch
Now that your local master
is up-to-date, switch back to your feature branch where you want to integrate these changes. Use git checkout your-feature-branch
.
5. Step 5: Merge master
into Your Feature Branch
This is the core step. You will merge the updated master
branch into your current feature branch. Use git merge master
.
6. Step 6: Resolve Any Merge Conflicts (If They Occur)
If Git cannot automatically merge the changes, it will report merge conflicts. You'll need to manually edit the conflicted files, choose which changes to keep, and then mark them as resolved using git add <conflicted-file>
.
7. Step 7: Commit the Merge
After resolving conflicts (or if there were none), Git will automatically create a merge commit. If you had conflicts, you'll need to explicitly commit after adding the resolved files. Git will usually provide a default commit message; you can accept it or modify it.
# 1. Ensure your working directory is clean (optional, but recommended)
git status
# If you have uncommitted changes, either commit them or stash them:
# git commit -m "WIP: Save current progress"
# git stash
# 2. Switch to master branch
git checkout master
# 3. Pull the latest changes from remote master
git pull origin master
# 4. Switch back to your feature branch
git checkout your-feature-branch
# 5. Merge master into your feature branch
git merge master
# 6. Resolve any merge conflicts (if they occur)
# Git will tell you which files have conflicts.
# Open these files, resolve the conflicts, then:
# git add <conflicted-file-1> <conflicted-file-2>
# 7. Commit the merge (Git will often do this automatically after resolving conflicts)
# If conflicts were resolved manually, you might need to commit explicitly:
# git commit -m "Merge branch 'master' into your-feature-branch"
Example Git commands for merging master into a feature branch
git pull --rebase origin master
on your master
branch instead of git pull origin master
if you want to avoid merge commits on your local master
and prefer a linear history. However, be cautious with rebasing if you've already pushed your local master
.Merge vs. Rebase: An Alternative Strategy
While merging master
into your feature branch is a common and safe approach, another strategy is to rebase
your feature branch onto master
. Rebasing rewrites your branch's history to appear as if you started your work from the latest master
commit, creating a linear history without merge commits.
When to use git merge
(as described above):
- You want to preserve the exact history of when changes were integrated.
- You are working on a shared feature branch where others might have based their work on your commits.
- Simplicity and safety are paramount, especially for beginners.
When to consider git rebase
:
- You prefer a clean, linear project history without extra merge commits.
- Your feature branch is private and hasn't been pushed or shared with others yet.
- You are comfortable with Git and understand the implications of rewriting history.
For the purpose of keeping your feature branch up-to-date with master
without rewriting your feature branch's history, git merge
is generally the recommended and safer option.
graph TD A[Feature Branch] --> B{Merge Master?} B -- Yes --> C[Merge Master into Feature] C --> D[Preserves History] C --> E[Creates Merge Commit] B -- No --> F{Rebase onto Master?} F -- Yes --> G[Rebase Feature onto Master] G --> H[Rewrites History] G --> I[Linear History] F -- No --> J[Continue Development]
Decision flow: Merge vs. Rebase for integrating master changes