Sub topic 2
The merge base and three-way merging
This chapter assumes you can commit, create and switch branches, and read git log. You should also know that a commit points to its parent. Here you will learn how Git decides what a merge should contain, and why that depends on a commit you never name.
Why Git needs a third commit
A merge combines two branch tips. Comparing only those two tips is not enough. Suppose one tip has timeout = 30 and the other has timeout = 60. Git cannot tell whether one side changed the line and the other left it alone, or whether both changed it. The answer decides whether Git can merge automatically.
Git solves this by adding a third commit, the merge base. This is the best common ancestor of the two tips, the latest point where the histories were still the same. Git compares each tip to the merge base to see what each side changed. This is called a three-way merge, and the three inputs are:
- base: the merge base commit
- ours: the tip of the branch you are on,
HEAD - theirs: the tip of the branch you are merging in
Finding the merge base
Take this history. Commit B is where feature branched off main.
The merge base is B. It is not A, because B is a more recent common ancestor. It is also not C or D, because each of those is reachable from only one branch.
Git can show you this commit directly:
git merge-base main featureThe command prints one commit hash. Because it is plain plumbing output, you can pass it to other commands, such as git show $(git merge-base main feature).
How the three-way merge works
Git runs a diff from the base to each tip. It then walks through the changed regions of the file (called hunks) and applies rules based on the three versions.
| Base to ours | Base to theirs | Result |
|---|---|---|
| unchanged | unchanged | Keep the base text |
| changed | unchanged | Take our change |
| unchanged | changed | Take their change |
| changed | changed, identical | Take the shared change |
| changed | changed, differently, overlapping | Conflict |
Only the last row stops the merge. Two people editing the same file does not cause a conflict. A conflict needs both sides to edit the same region in different ways. Git does this comparison line by line. It knows nothing about the meaning of your code.
Adjacent lines count as overlapping. If one branch changes line 4 and the other changes line 5, Git reports a conflict, even though the lines are different. Changes need at least one unchanged line between them to merge cleanly.
A worked example
This script builds the history above. It gives each branch a change in a different part of the same file, so the merge completes without a conflict. It assumes you have set user.name and user.email.
git init -b main demo && cd demo
printf 'timeout = 10\nhost = localhost\nport = 8080\nretries = 3\n' > config.txt
git add config.txt && git commit -m "Add config"
git switch -c feature
printf 'timeout = 10\nhost = localhost\nport = 8080\nretries = 5\n' > config.txt
git commit -am "Raise retries to 5"
git switch main
printf 'timeout = 30\nhost = localhost\nport = 8080\nretries = 3\n' > config.txt
git commit -am "Raise timeout to 30"
echo "merge base: $(git merge-base main feature)"
echo "first commit: $(git rev-parse main~1)"
git merge --no-edit feature
cat config.txt
git log --oneline --graphThe two echo lines print the same hash. The merge base is the "Add config" commit, which is the parent of both branch commits.
After the merge, config.txt holds both changes:
timeout = 30
host = localhost
port = 8080
retries = 5Here is how Git decided each line. For timeout, only main changed it, so Git took 30. For retries, only feature changed it, so Git took 5. The host and port lines are unchanged on both sides, so they stay. Neither tip alone contains this file. Git built it from the three versions.
The result is a new merge commit with two parents. git log --graph shows the two lines of history joining.
The merge base moves
The merge base is computed each time you merge. It is not stored on the branch. This has a practical effect. After you merge feature into main, the new base for any later merge of those two branches is the tip of feature that you merged. Git will not try to re-apply the earlier changes.
This is also why long-lived branches that you merge in regularly stay manageable. Each merge shortens the distance back to the base. A branch that has never been merged with main keeps its old base, so the diff between them keeps growing and so does the chance of overlapping edits.
Seeing what a merge will compare
Two commands use the merge base for you.
git diff main...feature(three dots) shows whatfeaturechanged since the merge base. It leaves out changes that landed onmainafter the branches split. This is what a merge would bring in fromfeature.git diff main..feature(two dots) compares the two tips directly. It mixesfeature's changes with the reverse ofmain's new changes, so it is a poor preview of a merge.
git diff main...feature
git log main..feature --onelineThe second command lists the commits on feature that are not yet in main.
When there is more than one merge base
Usually the history has one best common ancestor. Sometimes it has several. This happens after a criss-cross merge, where two branches merge into each other and then both continue. git merge-base prints one candidate by default. To see all of them, use:
git merge-base --all main featureGit's default merge strategy, ort (the default since Git 2.34), handles several bases by first merging the bases with each other into a temporary "virtual" base. It then uses that virtual base for the real merge. You never see this commit. It is the reason a criss-cross history can produce a conflict inside text that neither branch visibly edited. If a conflict looks wrong for the changes you can see, run git merge-base --all first.
Common mistakes
- Assuming the base is the tip of the other branch. The base is the common ancestor, not
main's latest commit. If you merge two tips and are surprised by the result, print the merge base and read the diffs from it. - Expecting a conflict whenever two people touch a file. Edits in separate regions merge cleanly, as in the example.
- Trusting a clean merge as correct. Git checks only whether text regions overlap. Two clean changes can still break the program together, for example when one branch renames a function and the other adds a call to the old name. Build and test after every merge.
- Reading a two-dot diff as a merge preview. Use three dots.
Summary of the model
Git takes the merge base, then the two tips. It computes what each side changed since the base, and combines the changes that do not overlap. Where both sides changed the same region differently, it stops and asks you. The next chapters cover what Git does when no merge is needed (fast-forward) and how to read the conflicts that remain.
Exercise
In a repository, main and feature split at commit B. Since then, main changed line 2 of app.txt and feature changed line 3 of the same file. Will git merge feature on main conflict? What if feature had changed line 2 instead? Which command shows you the commit Git uses as the comparison point?
Show answer
Changing line 2 on one side and line 3 on the other will conflict, because adjacent lines count as overlapping. If the changes had been two or more lines apart, the merge would complete automatically. If feature had changed line 2, the outcome would depend on the content. If both changed it to the identical text, Git takes the shared change. If they changed it differently, it conflicts. The command git merge-base main feature prints the merge base.
Exercise
You run git diff main..feature before a merge and see changes to files that you know feature never touched. Why does this happen, and what command gives a better preview of what the merge would bring in?
Show answer
Two dots compare the two tips directly. Changes made on main after the branches split show up as differences, shown in reverse, as if feature had made them. git diff main...feature (three dots) compares feature to the merge base, so it shows only what feature changed.