Sub topic 10
Settings for diffs and merge conflicts
This chapter assumes you can resolve a merge conflict by hand, know what a rebase is, and can read a unified diff. It covers three settings that make conflicts and diffs easier to read: merge.conflictStyle, diff.colorMoved and rerere.enabled.
Why these settings matter
Git's defaults show you the minimum. A conflict shows only "ours" and "theirs", so you must guess what the original code looked like. A diff of a refactor shows moved code as one big deletion and one big addition. And if you resolve the same conflict twice, Git does not remember the first answer. Each setting below fixes one of these problems.
[merge]
conflictStyle = zdiff3
[diff]
colorMoved = default
[rerere]
enabled = trueYou can set the same values from the command line:
git config --global merge.conflictStyle zdiff3
git config --global diff.colorMoved default
git config --global rerere.enabled trueSeeing the common ancestor with conflictStyle
By default, a conflict has two sections.
<<<<<<< HEAD
timeout = 30
=======
timeout = 60
>>>>>>> featureYou can see that the two sides differ. You cannot see what the line was before either side changed it. With merge.conflictStyle = zdiff3, Git adds a third section between ||||||| and =======, showing the common ancestor.
<<<<<<< HEAD
timeout = 30
||||||| parent of feature
timeout = 45
=======
timeout = 60
>>>>>>> featureNow the story is clear. The original was 45. One side lowered it to 30, the other raised it to 60. You can make an informed choice instead of guessing.
The zdiff3 style is a variant of the older diff3 style. It moves lines that both sides share out of the conflict block, so the block is smaller. zdiff3 needs Git 2.35 or later. On older versions, use diff3.
Highlighting moved code with colorMoved
When you move a function from one file to another, a plain diff shows red lines in one place and green lines in another. You cannot tell whether anything changed. With diff.colorMoved = default, Git colors lines that were moved without changes in a different color from lines that were truly added or removed.
git diff
git show
git log -pAll three commands respect the setting. If you want to tell the difference between moved blocks that sit next to each other, try diff.colorMoved = zebra, which alternates colors for adjacent blocks. The default value is a good start.
Reusing conflict resolutions with rerere
The name rerere stands for "reuse recorded resolution". When enabled, Git records how you resolved each conflict. If the same conflict appears again, Git applies your earlier resolution automatically.
This helps most when you:
- rebase a long-lived branch repeatedly, so the same conflicts return each time
- abort a merge, then try it again later
- merge a branch to test it, discard the merge, and merge it again for real
The workflow looks like this:
Instead, here is the sequence as a diagram.
Git does not commit the result for you. It rewrites the files and tells you Resolved 'file' using previous resolution. You still review and stage the files.
The recorded resolutions are stored in .git/rr-cache in each repository. They are not shared through git push.
Common errors
- Using
zdiff3on an old Git. Git reports an invalid value formerge.conflictStyle. Checkgit --version, or switch todiff3. - Forgetting the third section exists. Tools and scripts that look for only
=======still work, but when you edit by hand you must delete the|||||||section as well as the markers. - Trusting a bad recorded resolution. If you resolved a conflict wrongly once, rerere repeats the mistake. Remove it with
git rerere forget <path>and resolve again. - Setting the value in the wrong scope. Check the origin with
git config --show-origin --get merge.conflictStyle.
Exercise
You resolved a conflict in app.conf incorrectly with rerere enabled, and the same wrong result keeps coming back on each rebase. Which command removes the recorded resolution so you can resolve it again?
Show answer
Run git rerere forget app.conf, then resolve the conflict again. Git records the new resolution.
Exercise
A conflict block shows three sections, and the middle one starts with |||||||. Which setting caused this, and what does the middle section contain?
Show answer
merge.conflictStyle set to diff3 or zdiff3. The middle section contains the text from the common ancestor, which is the version before either side changed it.