Sub topic 15
Reusing resolutions with rerere
This chapter assumes you can run git merge, read conflict markers, resolve a conflict by hand, and run git rebase. The earlier chapters on those topics cover the background.
What rerere does
rerere stands for "reuse recorded resolution". When it is enabled, Git remembers how you resolved each conflict. If the same conflict appears again, Git applies your earlier resolution to the working tree for you.
Some situations make the same conflict come back:
- You merge a branch, dislike the result, throw the merge away, and merge again later.
- You rebase a long-lived branch several times onto a moving
main, and the same commit conflicts each time. - You test-merge several topic branches into a throwaway integration branch, then redo the merge when one branch changes.
- You merge in one direction and later merge the other way. The conflict is the same, so the resolution still fits.
Without rerere, you resolve the same hunk by hand each time. With it, you resolve once.
Enabling rerere
rerere is off by default. Enable it for every repository you use:
git config --global rerere.enabled trueYou can also enable it for one repository by running git config rerere.enabled true inside it. Another way to turn it on is to create the directory .git/rr-cache.
An optional setting stages files that rerere resolved:
git config --global rerere.autoUpdate trueLeave rerere.autoUpdate off until you trust your recorded resolutions. With it off, you review each reused resolution before staging it.
How it works internally
Git keeps its records in .git/rr-cache. Each conflict gets its own directory, named by a hash of the conflicting hunks. The hash ignores the surrounding file content and treats the two sides symmetrically, so swapping ours and theirs gives the same ID. That is why a resolution can be reused when you merge in the opposite direction.
A directory can contain these files:
| File | Contents |
|---|---|
| `preimage` | The conflicted file, with markers, as Git first wrote it |
| `postimage` | The same file after you resolved it |
| `thisimage` | A temporary copy used while Git replays a recorded resolution |
The cycle has three steps:
- A merge conflicts. Git writes the
preimageand printsRecorded preimage for 'config.txt'. - You resolve the file, stage it, and commit. Git writes the
postimageand printsRecorded resolution for 'config.txt'. - A later merge produces a conflict with the same hash. Git applies the three-way difference between
preimageandpostimageto the new conflicted file and printsResolved 'config.txt' using previous resolution.
A worked example
This script builds the same conflict as the earlier chapters. It resolves the conflict once, discards the merge commit, and merges again. Run it in an empty directory.
git init -b main demo && cd demo
git config rerere.enabled true
printf 'timeout = 10\n' > config.txt
git add config.txt && git commit -m "Add config"
git switch -c feature
printf 'timeout = 60\n' > config.txt
git commit -am "Raise timeout to 60"
git switch main
printf 'timeout = 30\n' > config.txt
git commit -am "Raise timeout to 30"
# First merge: conflicts, and rerere records the preimage
git merge feature
# Resolve by hand, then commit
printf 'timeout = 45\n' > config.txt
git add config.txt
git commit --no-edit
# Throw the merge away and redo it
git reset --hard HEAD~1
git merge feature
cat config.txt
git status --shortThe first git merge prints Recorded preimage for 'config.txt'. The git commit prints Recorded resolution for 'config.txt'. The second merge prints Resolved 'config.txt' using previous resolution. and then Automatic merge failed.
cat config.txt shows timeout = 45 with no markers. git status --short still shows UU config.txt, because Git has updated the working tree but has not staged the file. The merge is not finished until you run git add config.txt and git commit --no-edit.
Inspecting and managing recordings
These subcommands cover day-to-day use:
| Command | What it does |
|---|---|
| `git rerere status` | Lists files rerere is tracking in the current merge |
| `git rerere diff` | Shows the current state of the resolution compared with the recorded preimage |
| `git rerere remaining` | Lists files that still have unresolved conflicts |
| `git rerere forget <path>` | Deletes the recorded resolution for the conflict in that path |
| `git rerere clear` | Discards the recorded state for the current merge |
| `git rerere gc` | Removes old records |
git rerere gc prunes resolved records after 60 days and unresolved ones after 15 days. You can change these limits with gc.rerereResolved and gc.rerereUnresolved. Git runs this cleanup during git gc.
Fixing a bad recorded resolution
rerere records whatever you committed. If you resolved a conflict wrongly, rerere will reapply the mistake every time. Correct it as follows.
- Start the merge again so the conflict reappears with the old resolution applied.
- Run
git rerere forget config.txt. This deletes the stored resolution and restores the conflict markers. - Resolve the file correctly, stage it, and commit.
git rerere forget only works while a merge or rebase with that conflict is in progress. If you do not have one, recreate the conflict with git merge or git rebase first.
To see the conflict as it was originally written, run:
git checkout --merge -- config.txtThis rebuilds the conflicted file from the index, including markers. It does not remove the recorded resolution, so run git rerere forget as well.
Using rerere with rebase
Rebase benefits most from rerere. A rebase replays commits one at a time, so a branch with many commits can stop several times. If you abort and restart the rebase, or rebase again after main moves, you resolve the same hunks again.
git config --global rerere.enabled true
git switch feature
git rebase main
# conflict: rerere reuses a recorded resolution if one exists
git add config.txt
git rebase --continueBecause the hash ignores which side is ours and which is theirs, a resolution recorded during a merge also applies during a rebase of the same change. The reverse holds as well.
Limits and gotchas
- Records are local.
.git/rr-cacheis not part of any push or clone. A teammate does not get your resolutions unless you copy the directory. - The conflict must match. If either side changes the conflicting lines even slightly, the hash differs and Git records a new conflict. Context lines outside the conflict can differ without affecting the match.
- It records the file, not your intent. A resolution that was right for one merge can be wrong if the surrounding code has changed. Always run your tests before committing a reused resolution.
- Binary files and delete/rename conflicts are not recorded. rerere handles only content conflicts in text files.
- Auto-staging hides mistakes. With
rerere.autoUpdateon, a bad resolution goes into the index with no pause for review.
Summary
Enable rerere.enabled once. Resolve each conflict normally and commit. Git stores the conflicted and resolved versions, matches future conflicts by hash, and rewrites the file for you. You still review the result, run git add, and finish the merge or rebase yourself.
Exercise
You enabled rerere and resolved a conflict in app.py. Later you merge the same branches again. Git prints "Resolved 'app.py' using previous resolution." but git status still lists app.py as unmerged. What must you do to finish, and why is the file still unmerged?
Show answer
Review the file (for example with git diff), run git add app.py, then git commit. rerere only updates the working tree. It does not stage the file, so the index still holds the unmerged entries until you add it. Setting rerere.autoUpdate true would stage it automatically, but that skips your review.
Exercise
You committed a merge and later notice the recorded resolution for config.txt was wrong. How do you stop rerere from reapplying it?
Show answer
Recreate the conflict by starting the merge or rebase again. Then run git rerere forget config.txt to delete the recorded resolution and restore the markers. Resolve the file correctly, run git add config.txt, and commit. rerere records the corrected resolution.