Sub topic 2
The merge base and three-way merging
This chapter assumes you can create commits and branches and read git log. It explains how Git decides what a merge should produce, which is the basis for understanding why conflicts happen later.
What the merge base is
Git does not merge two branches by comparing their tips directly. Two tips alone can't tell Git who changed what. If one file says timeout = 30 and the other says timeout = 60, Git can't tell whether one side edited the line or both did.
Git therefore finds the merge base: the most recent commit that both branches have in their history. It then compares each tip against that commit. This is a three-way merge, and it uses three inputs:
- The merge base (the common ancestor).
- Your current branch tip ("ours").
- The tip of the branch being merged ("theirs").
Here B is the merge base of C and D. You can ask Git for it directly:
git merge-base main featureThe command prints the commit ID of B. Use git show on that ID to see the commit.
How the three inputs are combined
For each part of each file, Git compares both tips with the base and applies these rules:
| Base vs. ours | Base vs. theirs | Result |
|---|---|---|
| Unchanged | Unchanged | Keep the base content |
| Unchanged | Changed | Take theirs |
| Changed | Unchanged | Take ours |
| Changed the same way | Changed the same way | Take the shared change |
| Changed | Changed differently | Conflict |
The last row is the only case Git can't decide alone. In every other row, the base shows which side made the change, so Git can combine the sides without asking you.
A worked example
Suppose the base has a file with three lines:
name = app
timeout = 10
retries = 3On main, someone changes timeout to 30. On feature, someone changes retries to 5. Compared with the base, each side edited a different line, so Git takes both edits:
name = app
timeout = 30
retries = 5No conflict occurs, even though both branches changed the same file. Git works on regions of lines, not whole files. A conflict needs both sides to change the same region (or neighboring lines) in different ways.
Finding the base when history is complicated
If branches have been merged into each other before, there may be more than one candidate for the common ancestor. Git's default strategy, ort, handles this case by itself. It merges the candidate ancestors first and uses the result as the base. You rarely need to think about this, but it explains why git merge-base can seem to pick an unexpected commit. To see all the candidates, run:
git merge-base --all main featureCommon mistakes
- Expecting a conflict whenever both branches touch a file. Git conflicts only when edits overlap, as the example above shows.
- Thinking the newer commit wins. Git doesn't use timestamps. It uses the base and the two tips.
- Forgetting that the base moves. After you merge
mainintofeature, the next merge base is the new, more recent common commit. Old changes are not replayed.
Exercise
Branch main changes line 1 of notes.txt from "a" to "b". Branch feature changes line 1 of notes.txt from "a" to "c". Why does merging feature into main conflict, and what would happen if feature had changed line 1 to "b" instead?
Show answer
Compared with the merge base ("a"), both sides changed line 1 and they changed it to different values ("b" and "c"), so Git can't pick one and reports a conflict. If feature also changed it to "b", both sides made the same change, so Git takes "b" and no conflict occurs.