TExcelTAD
Home › Git › Git Branch Merge

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.

bash · try it yourself
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 contact
Updating 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:

bash · try it yourself
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
ls
Merge 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:

bash · try it yourself
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.html
Auto-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:

bash · try it yourself
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.html
Auto-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 main into them often keeps conflicts small. The Merge Conflicts lesson goes deeper: tools, strategies and ours/theirs.

Test yourself

When does Git perform a fast-forward merge?

  1. When the files are small
  2. When the receiving branch has no new commits since the other branch split off
  3. Always
  4. When you use --no-ff

In a conflicted file, which part is between <<<<<<< HEAD and =======?

  1. The incoming branch's version
  2. The version from the branch you are on
  3. The common ancestor
  4. Git's suggested resolution

How do you finish a merge after fixing the conflicted files?

  1. git merge --continue only
  2. git add the files, then git commit
  3. git push
  4. git stash