Sub topic 5
How Git decides two conflicts are the same
This chapter assumes you can commit, branch, merge, and rebase, and that you can read conflict markers (<<<<<<<, =======, >>>>>>>). It also assumes you have rerere enabled with git config rerere.enabled true. It explains how Git matches a new conflict to a recorded one, so you can predict when a resolution will be reused and when it will not.
What gets compared
Git does not match conflicts by file name, by commit ID, or by branch name. It matches by the content of the conflict hunk, meaning the text between and including the conflict markers.
When a merge stops with a conflict, rerere looks at each conflicted file. It builds a normalized version of each conflict hunk and hashes the result with SHA-1. The hash becomes the name of a directory under .git/rr-cache/. If that directory already holds a recorded resolution, rerere reuses it. If not, it saves the conflicted file as a new preimage in a new directory.
Two conflicts are "the same" when their normalized hunks are byte-for-byte identical.
Normalization
Raw conflict markers carry details that change from merge to merge, such as branch labels and the order of the two sides. Rerere removes this noise before hashing.
- The text after the markers is dropped. For example,
>>>>>>> featureand>>>>>>> origin/featurehash the same. - The two sides are sorted. If one merge puts your change first and another puts it second, the hunk is reordered into a fixed order before hashing. This is why a conflict created by
git merge featureand the mirrored conflict created by merging in the other direction can share one resolution. - Only the conflicted region is hashed. Lines of the file outside the markers do not matter.
Because the hash covers only the hunk, you can reuse a resolution in a different file, on a different branch, or at a different position in the file. The surrounding text can change freely.
What breaks a match
Any change inside either side of the hunk produces a different hash, and rerere then treats it as a new conflict. Common causes are:
- Someone edits one of the conflicting lines in a later commit, even by adding a space.
- A reformatter or line-ending change touches the conflicting lines.
- The merge base changes, so that a different set of lines now conflicts.
- A third change lands in the same region, so the hunk now contains more lines.
In these cases Git leaves the markers in place and records a new preimage. You resolve it by hand, and rerere stores a second resolution next to the first.
Seeing the match
You can check the match yourself. After a conflict, list the cache:
ls .git/rr-cache/Each directory name is a hash. Run a second merge that produces the same hunk, and no new directory appears. Git reuses the existing one and prints Resolved 'settings.txt' using previous resolution.
If you see a new directory instead, the hunk differed somewhere. Compare the two preimage files to find the difference:
diff .git/rr-cache/<first-hash>/preimage .git/rr-cache/<second-hash>/preimageReplace <first-hash> and <second-hash> with the directory names from your listing. The diff output points at the line that changed.
A practical consequence
Since matching depends on the hunk text, rerere works best when the same two sets of changes meet repeatedly. A test merge that you throw away and repeat produces identical hunks, so it matches every time. A rebase that replays the same commit onto a slightly newer base usually produces identical hunks too, unless the upstream change touched the conflicting lines.
Exercise
You resolved a conflict, and rerere recorded it. Later, a teammate fixes a typo in one of the conflicting lines on the branch you are merging. You merge again. Does rerere reuse your resolution, and why?
Show answer
No. The typo fix changes the text inside one side of the conflict hunk, so the normalized hunk hashes to a different value. Rerere finds no matching directory in .git/rr-cache/, records a new preimage, and leaves the conflict for you to resolve by hand.
Exercise
You merge branch A into branch B and resolve a conflict. Later you merge B into A and get the same conflicting lines, but with the two sides swapped. Can rerere reuse the first resolution, and what normalization step makes this possible?
Show answer
Yes. Rerere sorts the two sides of each hunk into a fixed order before hashing, and it ignores the labels after the markers, so both merges produce the same hash and share one recorded resolution.