Git Rerere

Updated · 12 min read
hardGit 2.53gitrereremerge-conflictsrebaseinterview

git rerere ("reuse recorded resolution") is a Git feature that records how you resolve each merge conflict, and resolves the identical conflict automatically when it appears again. It is used when the same conflict comes back many times: rebasing a long-lived branch again and again, redoing a merge you undid, or doing a trial merge to see the conflicts early and the real merge later. "Useful Git Settings" introduced it; this page shows everything it can do.

It is like a sat-nav that remembers the detour you took around a closed road. The next time you reach the same closed road, it takes you around it without asking.

Syntax

Terminal
git config rerere.enabled true
git rerere status
git rerere diff
git rerere forget <path>
Command or settingDescription
git config rerere.enabled trueIt is used to turn rerere on. After that, it works automatically in merge, rebase, cherry-pick, revert and pull.
git config rerere.autoUpdate trueIt is used to also stage files that rerere resolved, so you only need to check and continue.
git rerere statusIt is used to list files whose conflicts rerere is recording.
git rerere remainingIt is used to list files that still have conflicts rerere couldn't resolve.
git rerere diffIt is used to show how the current resolution differs from the conflict.
git rerere forget <path>It is used to delete a wrong recorded resolution for a file.
git rerere gcIt is used to delete old records (unresolved after 15 days, resolved after 60 days, by default).

The Starting Point

Navika and Advitha both changed the late fee, on main and on Advitha's long-running branch feature/fees. Navika turns rerere on in this repository (use --global to turn it on everywhere):

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
git config rerere.enabled true
printf "%s\n" 'Term fee: $12,000' 'Late fee: $250' 'Exam fee: $800' > fees.txt
git add fees.txt
git commit --quiet -m "Add the fee list"
git switch --quiet -c feature/fees
sed -i.bak 's/$250/$275/' fees.txt && rm fees.txt.bak
git commit --quiet -am "Set the late fee to 275" --author="Advitha Anamaneni <advitha@example.com>"
git switch --quiet main
sed -i.bak 's/$250/$300/' fees.txt && rm fees.txt.bak
git commit --quiet -am "Set the late fee to 300"
Output
 

Example 1: A Trial Merge Records the Conflict

Advitha's branch won't be merged for weeks, but Navika wants to see and settle the conflict now. She does a trial merge:

Terminal
git merge feature/fees
Output
Auto-merging fees.txt
CONFLICT (content): Merge conflict in fees.txt
Recorded preimage for 'fees.txt'
Automatic merge failed; fix conflicts and then commit the result.

Explanation: the merge conflicted as usual, and rerere added one line: Recorded preimage for 'fees.txt'. The preimage is the conflict itself, with its markers, saved so that rerere can recognize it later.

Terminal
git rerere status
git rerere remaining
Output
fees.txt
fees.txt

Both commands list fees.txt: rerere is watching it, and it still has an unresolved conflict.

Example 2: Resolving and Recording

The team agrees on $300 for the late fee, plus a note. Navika writes the resolved file, and checks what rerere will record:

Terminal
printf "%s\n" 'Term fee: $12,000' 'Late fee: $300 (agreed with Advitha)' 'Exam fee: $800' > fees.txt
git rerere diff
Output
--- a/fees.txt
+++ b/fees.txt
@@ -1,7 +1,3 @@
 Term fee: $12,000
-<<<<<<<
-Late fee: $275
-=======
-Late fee: $300
->>>>>>>
+Late fee: $300 (agreed with Advitha)
 Exam fee: $800

Explanation: git rerere diff compares the recorded conflict with the file as it is now: the conflict markers and both sides are removed (-), and the resolution is added (+). This is exactly what rerere will replay next time.

Terminal
git add fees.txt
git commit --quiet --no-edit
git reset --quiet --hard HEAD~1
git log --oneline --graph --all
Output
Recorded resolution for 'fees.txt'.
* 78b652c (HEAD -> main) Set the late fee to 300
| * 5797f64 (feature/fees) Set the late fee to 275
|/
* 18e3993 Add the fee list

Explanation: when the merge was committed, rerere saved the postimage, the resolved version, and said Recorded resolution for 'fees.txt'. (--quiet hides only the commit's own summary). Then Navika threw the trial merge away with git reset --hard HEAD~1: main is back where it was, but the recorded resolution stays in .git/rr-cache.

Example 3: The Real Merge, Weeks Later

Meanwhile, main gets other work, and finally the branch is merged for real:

Terminal
echo 'Bus fee: $1,500' >> fees.txt
git commit --quiet -am "Add the bus fee"
git merge feature/fees
Output
Auto-merging fees.txt
CONFLICT (content): Merge conflict in fees.txt
Resolved 'fees.txt' using previous resolution.
Automatic merge failed; fix conflicts and then commit the result.

Explanation: the same conflict came back, and rerere replaced it with the recorded resolution: Resolved 'fees.txt' using previous resolution. Git still reports the merge as failed and stops, so that you can check the result before committing. rerere matched the conflict even though main has a new commit and a new line, because it compares only the conflicting part, not the whole file.

Terminal
cat fees.txt
git status --short
Output
Term fee: $12,000
Late fee: $300 (agreed with Advitha)
Exam fee: $800
Bus fee: $1,500
UU fees.txt

The file has the agreed fee and the new bus fee line, but it is still unmerged (UU) until Navika adds it. She checks it, adds it and commits:

Terminal
git add fees.txt
git commit --quiet --no-edit
git log --oneline --graph -4
Output
*   d40a3b3 (HEAD -> main) Merge branch 'feature/fees'
|\
| * 5797f64 (feature/fees) Set the late fee to 275
* | 607d120 Add the bus fee
* | 78b652c Set the late fee to 300
|/
rerere records a resolution and replays it

Example 4: Staging Automatically with rerere.autoUpdate

Where the same conflicts come back very often, as in repeated rebases, rerere.autoUpdate also stages the resolved files. Advitha rebases a copy of her branch onto Navika's earlier work, which brings the same conflict back once more:

Terminal
git config rerere.autoUpdate true
git switch --quiet -c advitha/rebase-test feature/fees
git rebase main~1
Output
Auto-merging fees.txt
CONFLICT (content): Merge conflict in fees.txt
error: could not apply 5797f64... Set the late fee to 275
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 'fees.txt' using previous resolution.
Could not apply 5797f64... # Set the late fee to 275

Explanation: replaying Advitha's commit onto main~1 (the bus fee commit) produced the same late fee conflict. rerere resolved it and, because of autoUpdate, staged it: Staged 'fees.txt' using previous resolution. The rebase still stops for her to check. Then:

Terminal
git status --short
GIT_EDITOR=true git rebase --continue
Output
M  fees.txt
[detached HEAD 7ba1da2] Set the late fee to 275
 Author: Advitha Anamaneni <advitha@example.com>
 1 file changed, 1 insertion(+), 1 deletion(-)
Successfully rebased and updated refs/heads/advitha/rebase-test.
Terminal
git log --oneline -3
Output
7ba1da2 (HEAD -> advitha/rebase-test) Set the late fee to 275
607d120 Add the bus fee
78b652c Set the late fee to 300

M shows the resolved file is staged, and git rebase --continue finished. (GIT_EDITOR=true accepts the commit message without opening an editor.)

Where the Records Live

Terminal
ls .git/rr-cache | wc -l | tr -d " "
ls .git/rr-cache/*
Output
1
postimage
preimage
thisimage

Explanation: each recorded conflict is a folder named after a hash of the conflict's text. Inside, preimage is the conflict and postimage the resolution; thisimage is a scratch copy rerere uses while comparing. If one conflict ends up with different recorded resolutions, numbered files such as preimage.1 appear next to them. The records are local: .git/rr-cache is never pushed, so each person builds their own.

Example 5: Forgetting a Wrong Resolution

If you recorded a wrong resolution, rerere would keep applying it. To make rerere forget it, recreate the conflict and run git rerere forget:

Terminal
git switch --quiet main
git reset --quiet --hard HEAD~1
git config rerere.autoUpdate false
git merge feature/fees
Output
Auto-merging fees.txt
CONFLICT (content): Merge conflict in fees.txt
Resolved 'fees.txt' using previous resolution.
Automatic merge failed; fix conflicts and then commit the result.
Terminal
git rerere forget fees.txt
git checkout -m fees.txt
grep -c "<<<<<<<" fees.txt
git merge --abort
Output
Updated preimage for 'fees.txt'
Forgot resolution for 'fees.txt'
Recreated 1 merge conflict
1

Explanation: git rerere forget fees.txt deleted the recorded resolution for this conflict and saved the conflict again as a fresh preimage. It doesn't touch the file, which still has the old resolution, so git checkout -m fees.txt recreated the conflict markers from the merge (one conflict, as grep -c counts), ready to be resolved properly. The next resolution you commit is recorded in its place. Here Navika simply cancels the merge.

Common Mistakes

Recording a wrong resolution. rerere then applies the mistake every time. Use git rerere forget <path> while the conflict is present.

Committing without checking. With autoUpdate, it is easy to git rebase --continue without looking. Look at the diff and run the tests, as with any merge.

Expecting rerere to work across computers. Records are in .git/rr-cache, which isn't shared. (Git's contrib/rerere-train.sh script can teach rerere from existing merge commits.)

Turning rerere on only after the conflict. It must be on when the conflict first appears and when you resolve it.

Under the Hood

When a merge leaves conflict markers in a file, rerere normalizes the conflicting sections (it ignores the branch names after the markers and sorts the two sides into a fixed order) and hashes the result. That hash names a folder in .git/rr-cache, where it stores the conflicted text as preimage. When you commit, it saves the file's current content as postimage. Next time a conflict produces the same normalized text, rerere finds the folder and applies the change from preimage to postimage to your file, like a small three-way merge, which is why the rest of the file can differ. Because the sides are sorted, the same conflict is recognized even when the two branches swap places, as happens between a merge and a rebase.

Interview Corner

What does git rerere do?

It records how you resolve each merge conflict, and when the identical conflict appears again, in a merge, rebase or cherry-pick, it applies the same resolution automatically.

When is rerere useful?

When the same conflict comes back repeatedly: rebasing a long-lived branch many times, redoing an undone merge, or doing trial merges to resolve conflicts early and merging for real later.

Does rerere commit for you?

No. It applies the resolution and stops, so you can review it. With rerere.autoUpdate, it also stages the file, but you still continue or commit yourself.

How do you undo a wrong recorded resolution?

Recreate the conflict and run git rerere forget <path>, then resolve it correctly and commit.

Practice

Challenge: a second trial. With rerere on and autoUpdate off, run git merge feature/fees in Navika's repository again. The resolution was forgotten, so resolve the conflict with $300 and a note, commit, and print the Late fee line.

What does Git print? Before you open the solution, decide whether rerere resolves the conflict this time.

Show solution
Terminal
git merge feature/fees > /dev/null 2>&1
printf "%s\n" 'Term fee: $12,000' 'Late fee: $300 (agreed again)' 'Exam fee: $800' 'Bus fee: $1,500' > fees.txt
git add fees.txt
git commit --quiet --no-edit
grep "Late fee" fees.txt
Output
Recorded resolution for 'fees.txt'.
Late fee: $300 (agreed again)

rerere didn't resolve it, because the resolution was forgotten, so the merge stopped with a conflict (its messages are hidden here with > /dev/null 2>&1). When she committed, rerere recorded the new resolution (Recorded resolution for 'fees.txt'.), so the next time this conflict appears, it will use $300 with the new note.

Checkpoint. Turn on rerere globally. In a practice repository, create a conflict, resolve it, undo the merge with git reset --hard HEAD~1, and merge again to watch "Resolved ... using previous resolution".

Quick check: git rerere

4 questions
1What does rerere stand for?
2Which setting turns rerere on?
3A recorded resolution is wrong. What do you run while the conflict is present?
4Where does rerere store its records?

Key Takeaways

Quick reference

CommandIt is used to
git config --global rerere.enabled trueTurn rerere on
git config --global rerere.autoUpdate trueAlso stage resolved files
git rerere statusList recorded conflicts in progress
git rerere diffShow the resolution so far
git rerere forget <path>Forget a resolution

Was this tutorial helpful?

Lesson 96 of 106