Sub topic 4
What rerere stores in .git/rr-cache
This chapter assumes you can resolve conflict markers by hand and have already enabled rerere.enabled, as covered earlier. Here you look at the files rerere writes, so you know what "recorded" means on disk.
The rr-cache directory
Once rerere is enabled, Git creates .git/rr-cache/ the first time a conflict is recorded. The directory is local to the repository, and git clone does not copy it.
Each conflict gets its own subdirectory. The name is a 40-character hexadecimal hash derived from the conflicting text. The next chapter covers how that hash is computed. For now, treat it as the identity of one conflict.
Inside each subdirectory you will find up to two files:
| File | Contents | Written when |
|---|---|---|
| `preimage` | The file as Git left it, with conflict markers | The conflict first appears |
| `postimage` | The same file after you resolved it | You finish the resolution and Git runs rerere again |
The names describe the order in time. The preimage is the file before your resolution, and the postimage is the file after it.
A directory with only a preimage is a conflict that rerere has seen but not yet learned how to resolve. A directory with both files is a complete record that rerere can reuse.
Seeing it on disk
Run this in the rerere-demo repository from the walkthrough chapter, after you have resolved and committed the first merge:
ls .git/rr-cache/
ls .git/rr-cache/*/The first command lists one directory per recorded conflict. The second lists the files inside each. In the demo there is one conflict, so you see one hash and two files, postimage and preimage.
You can open both files like any text file. Comparing them shows exactly what your hand-made resolution changed:
diff .git/rr-cache/*/preimage .git/rr-cache/*/postimageThis only works when there is a single recorded conflict. With several, replace the * with a specific hash.
When each file is written
Rerere acts at two moments in a merge, rebase, or cherry-pick:
- When Git stops on a conflict, rerere writes the
preimage. It printsRecorded preimage for 'settings.txt'. - When you commit or continue the operation after resolving, rerere compares the working file to the earlier conflict. It writes the
postimageand printsRecorded resolution for 'settings.txt'.
Staging the file with git add alone does not write the postimage. The resolution is recorded the next time rerere runs, for example during git commit or git rebase --continue. You can also run git rerere yourself to trigger it.
Why two files and not one
You might expect rerere to save only the final text. It saves both because it needs to reuse the fix when the surrounding file has changed.
When the same conflict shows up again, rerere takes the new conflicted file, the old preimage, and the old postimage. It runs a three-way merge between them. The change from preimage to postimage is your resolution. That change is applied to the new file. A plain copy of the resolved file would overwrite unrelated edits elsewhere in the file. The three-way merge avoids that.
During this replay Git may also create a temporary thisimage file in the conflict directory. It holds the current conflicted version while the merge runs, and you can ignore it.
What the cache does not contain
- It does not store commit IDs or branch names. Matching depends on the text of the conflict only.
- It does not store your reasoning. A postimage records what the file looks like now, not why you chose it.
- It does not mark a resolution as correct. Rerere reuses whatever you committed, including mistakes. A later chapter shows how to remove a bad record.
Exercise
After a conflicting merge, .git/rr-cache/<hash>/ contains only a preimage. You then fix the file and run git add but do not commit. Is a postimage written yet, and when will it be?
Show answer
No. git add does not write it. Rerere writes the postimage the next time it runs after the resolution, such as during git commit or git rerere. Until then the directory holds only the preimage, so rerere cannot reuse the resolution.