Sub topic 11
Showing the base with diff3 and zdiff3
This chapter assumes you can start a merge, find conflict markers in a file, and edit and stage a resolution. It covers one setting that makes conflicts easier to read: the conflict style.
Why the default markers can mislead
By default, Git shows only two versions of a conflicted region: yours (HEAD) and theirs. You see the end result of each side but not the starting point. When both sides changed the same line, you can't tell what each side actually changed.
Take the config.txt conflict from earlier in this tutorial. The base had timeout = 10. One branch raised it to 30 and the other to 60. The default markers hide the original value:
<<<<<<< HEAD
timeout = 30
=======
timeout = 60
>>>>>>> featureFor one line this is manageable. For a region of 40 lines, you must work out the difference by hand.
Turning on the base section
The merge.conflictStyle setting adds a third section: the merge base, which is the common ancestor of the two branches. Set it globally:
git config --global merge.conflictStyle zdiff3zdiff3 needs Git 2.35 or later. On older versions, use diff3.
Now repeat the merge. The markers look like this:
<<<<<<< HEAD
timeout = 30
||||||| 3f2a1c9
timeout = 10
=======
timeout = 60
>>>>>>> featureThe file now has four marker lines:
| Marker | Meaning | Column | Column | Column | Column | Column | Column | Column |
|---|---|---|---|---|---|---|---|---|
| `<<<<<<< HEAD` | Start of your version (the current branch) | |||||||
| `\ | \ | \ | \ | \ | \ | \ | <commit>` | Start of the base version |
| `=======` | End of the base, start of their version | |||||||
| `>>>>>>> feature` | End of their version (the branch being merged) |
The base shows 10. Now you can see that both branches raised the value, to different numbers. That is a decision about which number is right, not a mechanical edit.
How to use the base
Compare each side against the base, not against the other side. Ask what each branch changed, then decide how to combine the changes.
- If only one side differs from the base, keep that side. The other side did not change the region.
- If both sides made the same change, keep it once.
- If both sides changed it differently, as with
30and60, you must choose or combine. The base tells you the intent of each change but not the right answer.
diff3 versus zdiff3
Both styles show the base. They differ in how they handle lines that are identical on both sides at the edges of a conflict.
diff3includes those shared lines inside all three sections. Regions look larger and repeat the same text.zdiff3("zealous diff3") moves identical lines at the start and end of the region out of the markers. The conflict shrinks to the lines that really differ.
For this reason, zdiff3 is usually the better choice when your Git version supports it.
Remove the base section before committing
The ||||||| section is only a reference. When you edit the file to its final content, delete all four marker lines and the base text. Then stage the file and commit as usual:
git add config.txt
git commitA leftover marker line will be committed as ordinary text, so search the file for <<<<<<<, |||||||, ======= and >>>>>>> first.
Exercise
A conflict region shows timeout = 30 under HEAD, timeout = 10 in the base section, and timeout = 10 under the other branch. What should the resolved line be, and why?
Show answer
timeout = 30. The other branch's version equals the base, so that branch did not change the line. Only HEAD changed it, so keeping HEAD's change is correct.