Sub topic 15
Reusing resolutions with rerere
This chapter assumes you can run a merge, read conflict markers, and resolve a conflict by hand. You should also know basic rebase usage.
What rerere does
rerere stands for "reuse recorded resolution". When it is enabled, Git remembers two things about each conflict you resolve: the conflicted text, and the result you committed. If the same conflict appears again, Git applies your earlier resolution to the working tree for you.
This helps in a few common situations:
- You abort a merge, then retry it later.
- You rebase a long-lived branch onto
mainseveral times, and the same conflicts return each time. - You merge a topic branch to test it, throw the merge away, and merge it again later.
Enable it once for all repositories:
git config --global rerere.enabled trueHow it works
Git stores records in .git/rr-cache. Each conflict gets an ID based on the conflicting hunks, so the record applies wherever the same hunks recur, even on different commits.
The record has two parts. The preimage is the file with conflict markers. The postimage is the file after you resolved it. Git saves the postimage when you commit the resolution.
A worked example
This reuses the conflict setup from earlier in the tutorial. Run it in a fresh directory.
git config rerere.enabled true
git init -b main demo && cd demo
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"
git merge featureThe merge stops, and Git prints a line such as Recorded preimage for 'config.txt'. Resolve the file by hand:
printf 'timeout = 45\n' > config.txt
git add config.txt
git commit -m "Merge feature, use timeout 45"Git prints Recorded resolution for 'config.txt'. Now undo the merge and repeat it:
git reset --hard HEAD~1
git merge featureGit reports Resolved 'config.txt' using previous resolution. The file now contains timeout = 45 with no markers.
Check the result
Useful commands:
git rerere statuslists files that rerere is tracking.git rerere diffshows the current state of the resolution against the recorded preimage.git rerere forget config.txtdiscards the saved resolution if it was wrong. Run it while the file is in a conflicted state.git rerere gcremoves old records. Unresolved ones expire after 15 days and resolved ones after 60 days by default.
Common errors
- A bad resolution is reused. Git replays whatever you committed last time. Use
git rerere forget <path>and resolve again. - Nothing is recorded. Records only exist from the moment you enable the setting. Conflicts resolved earlier are not recorded.
- Records are not shared.
.git/rr-cacheis local, so teammates do not receive your resolutions.
Exercise
You enabled rerere, resolved a conflict, and committed the merge. Later you notice the saved resolution is wrong. After you retry the merge and the conflict reappears with the wrong content already filled in, which command removes the bad record for config.txt, and what must you do afterward?
Show answer
Run git rerere forget config.txt. This discards the saved resolution. Then re-edit the file to the correct content, run git add config.txt, and commit so Git records the new resolution.