Sub topic 1
What git merge does
Prerequisites
This chapter assumes you can do the following:
- Run
git init,git clone,git add,git commit,git statusandgit log. - Create and switch branches with
git switch. - Use a command line and a text editor.
You need Git 2.23 or later for git switch. Every command here runs in a throwaway repository, so nothing you do will touch real work.
What a merge does
git merge combines the history of another branch into the branch you are on. You name the branch to bring in. Git changes the current branch and leaves the named branch alone.
git switch main
git merge featureAfter this, main contains the work from feature. The feature branch still points at the same commit as before.
A merge does two jobs:
- It joins the commit histories, so that Git remembers both lines of work came together.
- It combines the file contents from both lines into one working tree.
Most of the time Git does both jobs by itself. A conflict happens only when Git cannot decide how to combine the file contents. The rest of this tutorial covers that case. This chapter covers the normal case first, because you need it to understand the exceptions.
The merge base and three-way merging
Git does not compare the two branch tips directly. Two files that differ tell you nothing about who changed what. Git needs a third point: the most recent commit that both branches share. This commit is the merge base.
You can ask Git to show it:
git merge-base main featureThe command prints the hash of the merge base. In the diagram below, commit B is the merge base of main and feature.
Git then performs a three-way merge. It compares three versions of every file:
- The base version, from the merge base.
- The current branch version ("ours").
- The other branch version ("theirs").
For each part of each file, Git applies a simple rule:
| Base vs. ours | Base vs. theirs | Result |
|---|---|---|
| Same | Same | Keep the base text. |
| Same | Changed | Take theirs. |
| Changed | Same | Take ours. |
| Changed the same way | Changed the same way | Take the shared change. |
| Changed one way | Changed a different way | Conflict. |
The last row is the only one that stops the merge. A change on one side alone is never a conflict, because the base tells Git that the other side did not touch it. Without the base, Git could not tell a deliberate edit from a stale copy.
Git works on regions of lines, not whole files. Two people can edit the same file and merge cleanly, as long as their edits touch different, non-adjacent regions.
Fast-forward merges and merge commits
Git picks one of two strategies depending on how the histories relate.
Fast-forward merges
If the current branch has no commits of its own since the split, the current tip is the merge base. Nothing needs combining. Git moves the branch pointer forward to the other tip. This is a fast-forward merge, and it creates no new commit.
git init -b main ff-demo && cd ff-demo
printf 'one\n' > notes.txt
git add notes.txt && git commit -m "Add notes"
git switch -c feature
printf 'one\ntwo\n' > notes.txt
git commit -am "Add second line"
git switch main
git merge feature
git log --onelineThe merge prints Fast-forward. The log shows a straight line of two commits, with no merge commit.
Merge commits
If both branches have new commits, a fast-forward is impossible, since the pointer cannot move to a tip that does not contain your own commits. Git runs the three-way merge and records the result in a merge commit. A merge commit is a commit with two parents: the previous tip of the current branch and the tip of the merged branch.
Compare the two strategies:
| Row | Fast-forward | Merge commit |
|---|---|---|
| When it applies | Current branch has no new commits | Both branches have new commits |
| New commit created | No | Yes, with two parents |
| Shows that a branch existed | No | Yes |
| Can conflict | No | Yes |
A fast-forward never conflicts. Only a true merge commit can.
Controlling the strategy
You can override the default behavior with two flags:
git merge --no-ff featurealways creates a merge commit, even when a fast-forward is possible. Teams use it to keep a visible record of each feature branch.git merge --ff-only featurerefuses to merge unless a fast-forward is possible. It fails with an error instead of creating a merge commit. This is a safe way to update a branch that should never get merge commits.
A merge that succeeds
Here is a three-way merge with no conflict. Each branch edits a different file after the split.
git init -b main clean-demo && cd clean-demo
printf 'timeout = 10\n' > config.txt
printf 'hello\n' > readme.txt
git add . && git commit -m "Initial files"
git switch -c feature
printf 'hello, world\n' > readme.txt
git commit -am "Improve readme"
git switch main
printf 'timeout = 30\n' > config.txt
git commit -am "Raise timeout"
git merge feature --no-editGit reports that it made the merge using the ort strategy. The result is a new merge commit containing both changes. Check it:
git log --oneline --graph
git show --stat HEADThe graph shows the two lines of work rejoining. The merge commit's parents are listed by git show, under a Merge: line with two abbreviated hashes.
When the merge stops
The same command gives a different result when both branches change the same lines. The next chapter sections set up exactly this case:
git init -b main demo && cd demo
printf 'timeout = 10\n' > config.txt
git add config.txt && git commit -m "Add config"
git switch -c feature
printf 'timeout = 60\n' > config.txt
git commit -am "Raise timeout to 60"
git switch main
printf 'timeout = 30\n' > config.txt
git commit -am "Raise timeout to 30"
git merge featureGit prints CONFLICT (content): Merge conflict in config.txt and Automatic merge failed; fix conflicts and then commit the result. Both branches changed the line timeout = 10, to different values. Git cannot know which value is right, so it stops and asks you.
At this point the merge is in progress. It is not finished and it is not failed. Git has written what it could and left the rest for you. You can inspect it, finish it, or cancel it. Later chapters cover each option.
Common errors at this stage
- Merging in the wrong direction.
git merge featurechanges the branch you are on. Rungit branch --show-currentfirst if you are unsure. - Merging with uncommitted changes. Git may refuse if your local edits touch files the merge needs to change. Commit or stash first.
- Expecting a merge commit and getting none. A fast-forward leaves no merge commit. Use
--no-ffif you want one. - Reading a conflict as a failure. A conflict means the two branches disagree on the same lines. It is normal and recoverable.
Exercise
You are on main. You have made no commits on main since creating feature, and feature has two new commits. What does git merge feature do, and can it produce a conflict?
Show answer
It performs a fast-forward merge. Git moves the main pointer to the tip of feature and creates no merge commit. It cannot conflict, because the merge base is the current tip of main and there are no competing changes to combine.
Exercise
Both main and feature have new commits since they split. main edited line 1 of a.txt. feature edited line 20 of a.txt and also edited b.txt. Will git merge feature conflict, and why?
Show answer
No, it will not conflict. The edits to a.txt are in separate regions, and b.txt changed only on one side. For each region, only one side differs from the merge base, so Git takes that side. The result is a merge commit with two parents.
Exercise
What are the three versions of a file that Git compares in a three-way merge, and which commit supplies the first one?
Show answer
The base version, the current branch's version ("ours"), and the other branch's version ("theirs"). The merge base supplies the base version. It is the latest commit that both branches share, and git merge-base <a> <b> prints it.