DEV Community

Cover image for Git's Conflict Memory Doesn't Keep the File Name
Mustafa ERBAY
Mustafa ERBAY

Posted on Originally published at mustafaerbay.com.tr

Git's Conflict Memory Doesn't Keep the File Name

This blog's repository currently has seventeen linked worktrees next to the main
checkout:

$ git worktree list | wc -l
      18
Enter fullscreen mode Exit fullscreen mode

The first line in that list is the main checkout; the remaining seventeen are
linked trees — each one a job branching off the same main and touching the same
two or three files. In a setup like this your hand reaches for git rerere by
itself. My own runbook says as much: the rebase step of my article pipeline has a
line reading "on conflict, re-apply your mark in the calendar file." So repeated
conflicts aren't theoretical for me; they're weekly.

Two days ago I wrote about patch landing a hunk on a look-alike
block
.
That was a story about a matcher: having lost its target, it settled on the
closest resemblance. rerere isn't a matcher, it's a memory — what it stores
is not text but a decision I made. So the question I had was a different one:
when a decision is stored, does the thing that makes it mine — which file I made
it for
— go into the record?

It doesn't. And that isn't even the interesting part. When I give two files two
different answers, Git stores both; when the conflict comes back it still
picks the first one and writes that into the second file as well. The record
keeps my distinction; the lookup throws it away.

Every output below comes from small repositories created from scratch on my own
machine (git 2.48.1, macOS 26.6.2). The scripts ran with
GIT_CONFIG_GLOBAL=/dev/null so my own settings couldn't leak into the
measurements.

The smallest test

Here is the smallest rig. Two files, each with the same line in the middle; on
one branch that line becomes ANA, on the other YAN:

git init -q . && git config rerere.enabled true
printf 'satir1\nORTAK\nsatir3\n' > a.txt
printf 'satir1\nORTAK\nsatir3\n' > b.txt
git add . && git commit -qm base
git checkout -qb yan
printf 'satir1\nYAN\nsatir3\n' > a.txt
printf 'satir1\nYAN\nsatir3\n' > b.txt && git commit -qam yan
git checkout -q master
printf 'satir1\nANA\nsatir3\n' > a.txt
printf 'satir1\nANA\nsatir3\n' > b.txt && git commit -qam ana
git merge yan
Enter fullscreen mode Exit fullscreen mode

The merge conflicted in both files, as expected. The interesting part is what's
inside the rr-cache directory:

Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Auto-merging b.txt
CONFLICT (content): Merge conflict in b.txt
Recorded preimage for 'a.txt'
Recorded preimage for 'b.txt'
Automatic merge failed; fix conflicts and then commit the result.

$ ls .git/rr-cache/
59d8936972d7dfb6a90c3f394e30dc72f3f24969
Enter fullscreen mode Exit fullscreen mode

Two files, two separate conflicts, one record directory. Now I resolve only
a.txt and don't lay a finger on b.txt:

$ printf 'satir1\nCOZUM-ELLE\nsatir3\n' > a.txt
$ git rerere
Recorded resolution for 'a.txt'.
Resolved 'b.txt' using previous resolution.
$ cat b.txt
satir1
COZUM-ELLE
satir3
Enter fullscreen mode Exit fullscreen mode

b.txt now carries the answer I wrote for a.txt. There's no bug here and
nothing underhanded — that is the design. But if you enable it without knowing
what the design is, the result arrives as a surprise.

What the record actually remembers

The answer is in the source code, and in a single comment block at that. At the
top of handle_path() in rerere.c
(v2.48.1) it says: if the conflict
is in diff3 style the common-ancestor version is discarded, "our side" and
"their side" are reordered so that whichever sorts alphabetically earlier comes
first (the code does this with strbuf_cmp, which is memcmp-based, so the
ordering is by bytes rather than by any alphabet), and the conflict ID is
computed as the hash of one side,
NUL, the other side, and a NUL concatenated together.

Three things are missing from that sentence: the file's name, the rest of the
file, and the common ancestor. The ID comes only from the text of the two
conflicting blocks. Four measurements confirm it:

What I changed What happened to the ID
Put the conflict in two separate files Same (a single rr-cache directory)
Set merge.conflictStyle to diff3 Same (59d89369…)
Reversed the merge direction (master into yan) Same, the resolution replayed
Created the repo with --object-format=sha256 Length 64 characters, not 40

That last row is a small detail, but I enjoyed it: the comment introduces the ID
as "an SHA-1 hash," while the code calls no fixed algorithm at all — in v2.48.1
it is the_hash_algo->update_fn, on today's main branch git_hash_update. Both
use the repository's object hash algorithm; in a SHA-256 repository the directory
name is 64 characters:

$ git init -q --object-format=sha256 . && ... && git merge yan
$ ls .git/rr-cache
8bece4c14f01bc0849db622beb3104213a46d330aa1f3e6df2839f7b8becbcf0
Enter fullscreen mode Exit fullscreen mode

The comment has fallen behind the code — one more reminder that in places like
this, reading the source beats trusting the comment on top of it.

How much similarity is enough

The ID ignoring the file name doesn't by itself mean "it gets applied
everywhere." Replaying a resolution isn't done by comparing hashes but with a
three-way merge: Git takes the preimage (the conflicted state) and the
postimage (the resolved state) from the record and applies the difference
between them to the file in your hand. So even when the ID matches, the patch
has to land.

Which raises the real question: how much text do two files need to share
around the conflict? Here is the measurement. Each file has three lines of its
own, the conflicting line is identical in both, and N shared lines sit above and
below the conflict:

Shared lines around the conflict Result
0 Resolution not applied, a second variant recorded
1 a.txt's answer written into b.txt
2 a.txt's answer written into b.txt
3 a.txt's answer written into b.txt

A single shared line above and below is enough for the answer to cross into the
neighbouring file — even though the stored preimage is entirely a.txt's text:

$ cat -n .git/rr-cache/*/preimage
     1  AA-ozel-1
     2  AA-ozel-2
     3  AA-ozel-3
     4  ortak-ust-1
     5  <<<<<<<
     6  ANA
     7  =======
     8  YAN
     9  >>>>>>>
    10  ortak-alt-1
    11  AA-son-1
    12  AA-son-2
    13  AA-son-3

$ cat -n b.txt
     1  BB-ozel-1
     2  BB-ozel-2
     3  BB-ozel-3
     4  ortak-ust-1
     5  A-COZUM
     6  ortak-alt-1
     7  BB-son-1
     8  BB-son-2
     9  BB-son-3
Enter fullscreen mode Exit fullscreen mode

I had to build this table twice, because my first measurement script lied to me:
I was generating the "zero shared lines" row with seq 1 $N, and on macOS
seq 1 0 doesn't produce empty output — it counts backwards.

$ seq 1 0
1
0
Enter fullscreen mode Exit fullscreen mode

So in the case I believed was zero, the files actually shared two lines and the
table was claiming "it applies even with no shared context." The measuring tool
can corrupt the thing it measures; the more surprising a result is, the more
reason there is to read the script again.

Variants exist, but the first match wins

More than one resolution can live under the same ID. When a second shape shows
up, Git opens numbered variants: preimage.1, postimage.1. That sounds right:
if two files have two different answers, there will be two variants and everyone
gets their own answer. The measurement says otherwise.

The rig: two workflow files. The same line conflicts, but I deliberately make two
different decisions — 45 minutes for the build job, 90 for the publish job:

$ git merge ust
Recorded preimage for 'is/derle.yml'
Recorded preimage for 'is/yayinla.yml'
# I write 45 into derle.yml, 90 into yayinla.yml, then commit
Recorded resolution for 'is/derle.yml'.
Recorded resolution for 'is/yayinla.yml'.

$ ls .git/rr-cache/*/
postimage  postimage.1  preimage  preimage.1
Enter fullscreen mode Exit fullscreen mode

Two variants recorded; so far so good. Now the same conflict comes back during a
rebase (with rerere.autoupdate on):

$ git rebase master
Rebasing (1/1)Auto-merging is/derle.yml
CONFLICT (content): Merge conflict in is/derle.yml
Auto-merging is/yayinla.yml
CONFLICT (content): Merge conflict in is/yayinla.yml
error: could not apply 234a516... ust 30
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Staged 'is/derle.yml' using previous resolution.
Staged 'is/yayinla.yml' using previous resolution.
Could not apply 234a516... ust 30

$ grep timeout is/derle.yml
    timeout-minutes: 45
$ grep timeout is/yayinla.yml
    timeout-minutes: 45
Enter fullscreen mode Exit fullscreen mode

yayinla.yml got 45, not 90. The reason is the loop in
do_rerere_one_path(): Git tries the variants in order starting from 0 and uses
the first one that merges cleanly; it never asks "which variant was recorded
for this path," because the record has no tie to any path. Since the two files'
conflicts are identical, variant 0 fits both.

Git isn't silent here: it prints two CONFLICT lines, says it could not apply
the commit, and stops the rebase. The one thing it doesn't say is whether the
answer it applied is the answer I gave for this file. And if you ask whether
anything is left for you to do, the answer is no:

$ git rerere remaining
$ git rerere status
$ git status --short
M  is/derle.yml
M  is/yayinla.yml

$ git rebase --continue
[detached HEAD 3ff2840] ust 30
 2 files changed, 2 insertions(+), 2 deletions(-)
Successfully rebased and updated refs/heads/ust.

$ git show HEAD:is/yayinla.yml | grep timeout
    timeout-minutes: 45
Enter fullscreen mode Exit fullscreen mode

The wrong value went into the commit. I stopped, looked, and carried on.
git status --short does list the two files — but it isn't the thing that tells
you which answer went in, and the two commands that might, rerere remaining
and rerere status, are both empty.

Their silence doesn't mean the work is done. rerere status reads the MERGE_RR
ledger, and a path rerere resolved itself drops off that ledger. rerere
remaining
looks from somewhere else entirely: rerere_remaining() walks the
index and adds only paths marked PUNTED — the ones rerere couldn't handle at all, such as
conflicting submodules; an ordinary content conflict counts as THREE_STAGED and
never gets in. The same thing happens with autoupdate off: with UU f.txt
sitting in the index — an unresolved conflict still in my hands — MERGE_RR was
empty and both commands printed nothing. So their silence means neither "the
resolutions are correct" nor "no conflicts left," only "rerere's own ledger is
empty."

Diagram

How much warning you get

rerere.autoupdate is off by default, and that's a good default. With it off the
file's content is resolved but the index entry stays unmerged:

$ git merge master
Resolved 'is/derle.yml' using previous resolution.
Resolved 'is/yayinla.yml' using previous resolution.
$ git status --short
UU is/derle.yml
UU is/yayinla.yml
Enter fullscreen mode Exit fullscreen mode

UU forces you to run git add, and that's a pause in which you can look at the
file. With it on, the message becomes "Staged … using previous resolution" and
git rebase --continue asks nothing at all. I won't be turning it on in my own
setup, and the reason fits in one sentence: the only thing it buys me is two
keystrokes, while the price is a resolution I never reviewed landing in a commit.

Even with it off, there's one thing to keep in mind: you don't need to run the
git rerere command for a resolution to be recorded. Resolving the conflict and
committing is enough; Git writes the record itself:

$ printf 'COZUM\n' > a.txt && git add a.txt && git commit -qm birlesme
Recorded resolution for 'a.txt'.
$ ls .git/rr-cache/*/
postimage  preimage
Enter fullscreen mode Exit fullscreen mode

Where it kicks in

A resolution replays not only in git merge but on every path that produces a
conflict through a three-way merge. All of them went against the same record:

  • git merge — replayed
  • git rebase — replayed
  • git cherry-pick — replayed
  • reversed merge (recording on master, repeating it on the yan branch) — replayed

The reversed-direction run makes the purpose of the side sorting
concrete: because "ours" and "theirs" are ordered alphabetically, which branch
you entered the conflict from doesn't change the ID. In daily life that helps —
the same record works whether you move through git pull --rebase or git merge.

Deleting the setting isn't enough

If rerere was ever used in a repository, it stays on even after you remove the
setting. The documentation says so: when rerere.enabled is unset, rerere is
considered enabled if there is an rr-cache directory under $GIT_DIR. The
measurement takes three lines:

# 1) no setting, no rr-cache -> rerere inactive
$ git merge ust
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.
$ ls -d .git/rr-cache
ls: .git/rr-cache: No such file or directory
$ git merge --abort

# 2) same repo: rr-cache directory created by hand, still no setting
$ mkdir .git/rr-cache && git merge ust
$ ls .git/rr-cache | wc -l; test -f .git/MERGE_RR && echo "MERGE_RR exists"
       1
MERGE_RR exists
$ git merge --abort

# 3) same repo: rerere.enabled false
$ git config rerere.enabled false && git merge ust
$ ls .git/rr-cache | wc -l; test -f .git/MERGE_RR || echo "no MERGE_RR"
       1
no MERGE_RR
Enter fullscreen mode Exit fullscreen mode

Note the third step: false doesn't delete the record, it only stops MERGE_RR
from being written — rerere goes quiet while its memory stays in place, ready for
the day someone removes the setting again. And "I tried rerere and then deleted
the setting" doesn't turn it off either; that takes rerere.enabled false or
deleting the rr-cache directory.

This is a surprise waiting for teams that copy the repository directory itself
(restoring a backup, rsyncing a tree). Not for clones: git clone creates no
rr-cache in the destination, in both local and --no-local runs. The record
doesn't travel over the network, it travels with a file copy.

How long records live

Records don't live forever, but they aren't cleaned up on their own either: when
git rerere gc runs, resolved records older than 60 days and unresolved ones
older than 15 days are pruned. Those two thresholds come from gc.rerereResolved
and gc.rerereUnresolved. Backdating the records' modification times gives four
runs:

Record Age After git rerere gc
Resolved 59 days Kept
Resolved 61 days Pruned
Unresolved 14 days Kept
Unresolved 16 days Pruned

The practical consequence: on a long-lived branch you haven't touched for two
months, you can't assume the record is still there. You don't even have to invoke
that cleanup yourself: rerere gc is among the subcommands git gc
runs
, so git gc
--auto
can trigger it in the background.

The undo kit

The good news is that undoing a replayed resolution is easy. Three commands are
enough; two of them appear in the run below:

# 1) bring the conflict back
$ git checkout --merge is/yayinla.yml
Recreated 1 merge conflict
$ cat -n is/yayinla.yml
     1  name: yayinla
     2  on:
     3    push:
     4  jobs:
     5    build:
     6  <<<<<<< ours
     7      timeout-minutes: 30
     8  =======
     9      timeout-minutes: 20
    10  >>>>>>> theirs
    11      steps:
    12        - run: make yayinla

# 2) forget the record
$ git rerere forget is/yayinla.yml
Updated preimage for 'is/yayinla.yml'
Forgot resolution for 'is/yayinla.yml'
Enter fullscreen mode Exit fullscreen mode

git checkout --merge <path> returns the file to its conflicted state (note that
the markers read ours/theirs — the branch names are gone). git rerere forget
<path>
deletes the recorded resolution for that conflict, so the wrong answer
won't replay again. The third command is git rerere diff: it shows what you
changed while resolving — but once a resolution has already been replayed its
output comes back empty, so don't count on it as an audit tool.

Is it worth enabling

That was my own question, and the answer got clearer once I knew what the ID is.
rerere recognises a conflict with the same content, not the same job. My
recurring conflict lives in the calendar file, and each run marks a different
entry — so the conflicting lines differ every time. I tested this not with the
calendar file itself but with a small JSON shaped like it: two separate pairs of
runs produced two separate IDs.

# kosu-a/kosu-b (konu-1, konu-2) and kosu-c/kosu-d (konu-3, konu-4)
$ ls .git/rr-cache | cut -c1-12
47af248a01c6
5b355e70b305
Enter fullscreen mode Exit fullscreen mode

So rerere doesn't touch my actual problem at all. On the other hand, if you carry
a long-lived topic branch on top of main for weeks and the same three lines
conflict the same way every week, the gain is real.

If you do decide to enable it, here's the order I found useful:

  1. rerere.enabled true, and leave rerere.autoupdate off — UU is your review pause.
  2. If the repository holds files that resemble each other (workflows generated from one template, per-tenant copies of a configuration), think first about whether the conflict has a single answer. In my repository (origin/main before this article landed) 6,692 of 6,707 tracked files have distinct content, so only 15 are byte-for-byte copies of another file — and all of them are images, not text. But the table showed that being an exact twin isn't required; one shared neighbouring line is enough.
  3. Read every replayed resolution before you run git add. "Resolved … using previous resolution" does not say that the resolution is the decision you made for this file.
  4. If it replays wrongly: git checkout --merge <path> plus git rerere forget <path>.

I had already paid the price of silencing a conflict on the merge-driver
side
: a merge=union
line in .gitattributes combined both sides without ever showing the conflict,
and the loss was quiet. rerere isn't that blunt; it prints its message and puts
the file in front of you. What misled me was assuming that what it stored was my
decision. It did store a decision — but what it recorded was text, and even while
holding two of my answers side by side it no longer knew which one belonged to
which file.

When a directory is named by a 40-character hash (64 in a SHA-256 repository),
it's worth asking what that hash leaves out: 59d89369… describes the text of two lines, not where my decision
about those two lines belonged. The difference showed up as two files that should
have read 45 and 90 both reading 45 — a small number, but the wrong one.

Official Sources

Top comments (0)