Git Branch Merge
Merging combines the work of another branch into the current one. You switch to the branch that should receive the changes (usually main) and run git merge <other-branch>.
Git chooses between two kinds of merge automatically.
Fast-forward merge
If main has not moved since the feature branch was created, there is nothing to combine: Git simply moves main forward to the feature's latest commit.
mkdir site && cd site && git init -q
echo "home" > index.html && git add . && git commit -q -m "Add home page"
git switch -q -c contact
echo "email us" > contact.html && git add . && git commit -q -m "Add contact page"
git switch -q main
git merge contact
git log --oneline --graph
git branch -d contactUpdating ff139b4..dccedc8
Fast-forward
contact.html | 1 +
1 file changed, 1 insertion(+)
create mode 100644 contact.html
* dccedc8 Add contact page
* ff139b4 Add home page
Deleted branch contact (was dccedc8).
"Fast-forward" means no new commit was created; the history stays a straight line.
Three-way merge
If both branches have new commits, Git creates a merge commit with two parents. It compares both tips with their common ancestor (hence "three-way") and combines the changes:
mkdir site && cd site && git init -q
echo "home" > index.html && git add . && git commit -q -m "Add home page"
git switch -q -c contact
echo "email us" > contact.html && git add . && git commit -q -m "Add contact page"
git switch -q main
echo "body {}" > style.css && git add . && git commit -q -m "Add stylesheet"
git merge contact -m "Merge branch 'contact'"
git log --oneline --graph
lsMerge made by the 'ort' strategy.
contact.html | 1 +
1 file changed, 1 insertion(+)
create mode 100644 contact.html
* 7fab52c Merge branch 'contact'
|\
| * dccedc8 Add contact page
* | 215bf8d Add stylesheet
|/
* ff139b4 Add home page
contact.html
index.html
style.css
The graph shows the two lines joining at the merge commit. git merge --no-ff forces a merge commit even when a fast-forward would be possible, which some teams prefer so each feature remains visible as a group.
Merge conflicts
When both branches changed the same lines of a file, Git cannot know which version you want. It stops, marks the file as conflicted and lets you decide:
mkdir site && cd site && git init -q
echo "Welcome to our shop" > index.html && git add . && git commit -q -m "Add home page"
git switch -q -c friendly
echo "Hi there, welcome!" > index.html && git commit -q -am "Friendlier greeting"
git switch -q main
echo "Welcome to Ada's Shop" > index.html && git commit -q -am "Use shop name"
git merge friendly
echo "--- status:"
git status -s
echo "--- the file now contains:"
cat index.htmlAuto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
--- status:
UU index.html
--- the file now contains:
<<<<<<< HEAD
Welcome to Ada's Shop
=======
Hi there, welcome!
>>>>>>> friendly
Between <<<<<<< HEAD and ======= is your current branch's version; between ======= and >>>>>>> friendly is the incoming one. UU in the short status means "unmerged, both modified".
Resolving a conflict
Edit the file so it contains what you really want (delete the markers), stage it, and commit to finish the merge:
mkdir site && cd site && git init -q
echo "Welcome to our shop" > index.html && git add . && git commit -q -m "Add home page"
git switch -q -c friendly
echo "Hi there, welcome!" > index.html && git commit -q -am "Friendlier greeting"
git switch -q main
echo "Welcome to Ada's Shop" > index.html && git commit -q -am "Use shop name"
git merge -q friendly
echo "Hi there, welcome to Ada's Shop!" > index.html
git add index.html
git commit -q --no-edit
git log --oneline --graph
cat index.htmlAuto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
* 719ee6e Merge branch 'friendly'
|\
| * 54d8b32 Friendlier greeting
* | cf25934 Use shop name
|/
* 5139e85 Add home page
Hi there, welcome to Ada's Shop!
--no-edit accepts the default message ("Merge branch 'friendly'"). If you'd rather give up, git merge --abort returns everything to the state before the merge started.
Tip: Conflicts are normal, not errors. Keeping branches short-lived and merging
maininto them often keeps conflicts small. The Merge Conflicts lesson goes deeper: tools, strategies andours/theirs.
Test yourself
When does Git perform a fast-forward merge?
- When the files are small
- When the receiving branch has no new commits since the other branch split off
- Always
- When you use --no-ff
In a conflicted file, which part is between <<<<<<< HEAD and =======?
- The incoming branch's version
- The version from the branch you are on
- The common ancestor
- Git's suggested resolution
How do you finish a merge after fixing the conflicted files?
- git merge --continue only
- git add the files, then git commit
- git push
- git stash