Sub topic 15
Reusing recorded resolutions with rerere
This chapter assumes you can run git rebase, resolve a conflict with --continue, and read git log. It covers rerere, a Git feature that remembers how you resolved a conflict and applies the same fix when the conflict shows up again.
Why conflicts repeat
Rebasing a long-lived branch replays each commit on a new base. If you abort and retry, or rebase the same branch again after main moves, you can hit the same conflict several times. Without help, you resolve it by hand each time.
rerere stands for "reuse recorded resolution". It is off by default.
Turning it on
git config --global rerere.enabled trueUse git config rerere.enabled true without --global to enable it for one repository only.
What Git records
When a merge or rebase stops on a conflict, rerere saves a snapshot of the conflicted file, called the preimage. After you resolve the conflict and commit (or run git rebase --continue), it saves the resolved file, called the postimage. Both are stored under .git/rr-cache, keyed by a hash of the conflict text.
If Git later meets a conflict with the same text, it applies the saved resolution to the working tree. It does not stage the result or continue for you.
A worked example
Suppose feature conflicts with origin/main in app.py.
git config rerere.enabled true
git switch feature
git rebase origin/main
# CONFLICT in app.py. Git prints: Recorded preimage for 'app.py'Edit app.py, then continue:
git add app.py
git rebase --continue
# Git prints: Recorded resolution for 'app.py'Now suppose you undo the rebase and try again, perhaps after changing your mind about squashing:
git reset --hard ORIG_HEAD
git rebase origin/main
# Git prints: Resolved 'app.py' using previous resolution.The file has no conflict markers. Check the result, stage it, and continue:
git diff
git add app.py
git rebase --continueUseful commands
| Command | Purpose |
|---|---|
| `git rerere status` | Lists files with recorded preimages in the current operation |
| `git rerere diff` | Shows the current resolution compared with the preimage |
| `git rerere forget path/to/file` | Discards the saved resolution for that file's current conflict |
| `git rerere gc` | Deletes old records |
Common errors
- Trusting a bad resolution. If you recorded a wrong fix,
rererereapplies it. Review withgit diffbefore staging. To discard it, rungit rerere forget path/to/fileand re-create the conflict withgit checkout --merge path/to/file. - Expecting automatic continuation.
rerereonly edits the working tree. You still rungit addandgit rebase --continue. - Assuming it is shared.
.git/rr-cacheis local. Teammates and other clones do not get your recorded resolutions. - Expecting a match after the text changes. The key is the conflict text. If either side changes, Git sees a new conflict.
Exercise
You enabled rerere and resolved a conflict in config.yml during a rebase. You abort and rebase again. Git reports "Resolved 'config.yml' using previous resolution." What must you still do before the rebase continues, and why?
Show answer
Review the file with git diff, run git add config.yml, then run git rebase --continue. rerere only updates the working tree. It does not stage files or continue the rebase, and the reused resolution might be wrong for the new situation.