Sub topic 11
Showing the base with diff3 and zdiff3
This chapter assumes you can start a merge, read basic conflict markers, and finish or abort a merge with git merge --abort. You should also know that a three-way merge compares two branch tips against their merge base. Here you will change how Git writes conflict markers so the base version appears next to both sides.
What the default markers hide
By default, Git shows only two versions of a conflicted region: yours (above =======) and theirs (below it). It leaves out the third input of the merge, the base. The base is the version of the file at the merge base commit.
Without the base, you can't always tell what each side did. Suppose you see this:
<<<<<<< HEAD
=======
cache = off
>>>>>>> featureYour side has nothing in that spot. You can't tell whether your branch deleted the cache line or never had it. Both explanations fit the markers, but they call for different resolutions.
The merge.conflictStyle setting controls how markers are written. It has three values:
| Style | Markers written | Notes | Column | Column | Column | Column | Column | Column | Column |
|---|---|---|---|---|---|---|---|---|---|
| `merge` | ours and theirs | The default. | |||||||
| `diff3` | ours, base and theirs | Adds a ` | ` section holding the base. | ||||||
| `zdiff3` | ours, base and theirs | Like `diff3`, but moves lines that both sides share out of the conflict. Needs Git 2.35 or later. |
The diff3 layout
With diff3, a conflicted region has three parts, separated by three marker lines:
<<<<<<< HEAD
(our lines)
||||||| 0a1b2c3
(base lines)
=======
(their lines)
>>>>>>> featureThe text after ||||||| is a label for the base. In a plain merge it identifies the base commit. During a rebase or cherry-pick, it usually reads like "parent of" followed by a commit. Treat the label as informational and don't parse it.
Here is the cache example again, with the base shown:
<<<<<<< HEAD
||||||| 0a1b2c3
cache = on
=======
cache = off
>>>>>>> featureThe meaning is now clear. The base had cache = on. Your branch deleted the line, and the other branch changed it to off. You are looking at a delete-versus-edit conflict, and you have to decide which intent wins.
How to read a three-part conflict
Compare each side to the base, one at a time:
- If only one side differs from the base, that side made the real change. Keep it.
- If both sides differ from the base in the same way, there is nothing to decide. Keep either one.
- If both sides differ from the base in different ways, you have to combine the two intents or pick one.
The default style can't support this comparison. You would have to run git show on the base yourself. The base section puts that information in the file you are already editing.
The difference between diff3 and zdiff3
When both sides make the same change at the edge of a conflicted region, diff3 keeps those shared lines inside the conflict. The reason is technical. Trimming them would make the base section misalign with the other two sections. The result is a larger conflict than you need to read.
zdiff3 (the "z" stands for "zealous") trims those shared lines from the top and bottom of the conflict and writes them outside the markers. The conflict is then smaller and easier to read.
Try it in a scratch repository. The script builds a conflict where both branches change mode the same way but change timeout differently:
git init -b main styles && cd styles
printf 'mode = fast\ntimeout = 10\n' > config.txt
git add config.txt && git commit -m "Add config"
git switch -c feature
printf 'mode = safe\ntimeout = 60\n' > config.txt
git commit -am "Safe mode, timeout 60"
git switch main
printf 'mode = safe\ntimeout = 30\n' > config.txt
git commit -am "Safe mode, timeout 30"
git -c merge.conflictStyle=diff3 merge feature
cat config.txt
git merge --abort
git -c merge.conflictStyle=zdiff3 merge feature
cat config.txt
git merge --abortThe -c option sets the style for that one command only. The diff3 run should leave a file like this:
<<<<<<< HEAD
mode = safe
timeout = 30
||||||| 0a1b2c3
mode = fast
timeout = 10
=======
mode = safe
timeout = 60
>>>>>>> featureThe zdiff3 run should leave this:
mode = safe
<<<<<<< HEAD
timeout = 30
||||||| 0a1b2c3
timeout = 10
=======
timeout = 60
>>>>>>> featureIn the second file, the mode = safe line sits above the markers. It has no conflict, so you don't need to look at it. The only decision left is the value of timeout. Your hash will differ from 0a1b2c3.
Turning the style on
Set it once for all repositories on your machine:
git config --global merge.conflictStyle zdiff3If your Git is older than 2.35, use diff3 instead. Older versions don't recognize zdiff3 and report an unknown style. Check your version with git --version.
Drop --global to apply the setting to one repository only. The setting affects every command that writes conflict markers, including git merge, git rebase, git cherry-pick and git stash pop.
Re-creating markers in a different style
Suppose you are already in a conflict and wish you had the base. You can regenerate the markers for a file:
git checkout --conflict=zdiff3 -- config.txtThis rewrites config.txt from the three index stages, using the style you name. Any edits you made to that file are discarded. Run it before you start editing, or save a copy of the file first.
If you only want to look at the base without changing the file, print it from the index:
git show :1:config.txtStage 1 is the base, stage 2 is ours and stage 3 is theirs. You met these stages in the chapter on inspecting a merge in progress.
Gotchas
- Remove the whole base section. When you finish editing, delete all four marker lines (
<<<<<<<,|||||||,=======and>>>>>>>) and every line of the base and the side you are discarding. A leftover|||||||line is easy to miss and can end up in a commit. - Check for leftovers.
git diff --checkreports conflict markers that remain in the working tree. Run it before you commit. - The base can be synthetic. If the two branches have more than one merge base, Git first merges those bases into a virtual one, and the base section shows that virtual result. It may not match any single commit.
- Tools that parse markers. Some editors, scripts and review plugins assume the two-sided layout. If one misbehaves after you change the style, test it on a scratch conflict.
- Style doesn't change the merge. The setting changes only how a conflict is written. Git decides what conflicts, and what merges cleanly, the same way under all three styles.
When to choose each style
Use zdiff3 if your Git supports it. It shows the base and keeps conflicts as small as possible. Use diff3 on older Git versions. Keep merge only if a tool you depend on can't handle the extra section.
Exercise
A conflicted region under diff3 shows "ours" with the line retries = 5, a base with retries = 3, and "theirs" with retries = 3. Why would Git treat this as a conflict, and what should the resolved line be?
Show answer
Git would not treat that single line as a conflict. Only one side changed it: ours changed 3 to 5, and theirs matches the base. Git would take retries = 5 automatically. If you see this region inside a conflict, it is because a neighbouring line also conflicts and the lines sit in the same hunk (diff3 does not trim unchanged edges, but zdiff3 would move such a line outside the markers). The resolved line is retries = 5.
Exercise
You are in the middle of resolving config.txt under the default style and want the base shown. What command re-creates the markers with the base, and what is the risk of running it?
Show answer
Run git checkout --conflict=zdiff3 -- config.txt (or --conflict=diff3 on Git older than 2.35). It rewrites the file from the index stages, so any edits you already made to that file are lost.