Sub topic 2
What rerere does: reuse recorded resolution
This chapter assumes you can commit, branch, merge, and rebase. It also assumes you can resolve conflict markers (<<<<<<<, =======, >>>>>>>) by hand in a terminal.
The idea
git rerere stands for "reuse recorded resolution". It watches how you resolve a merge conflict and saves that resolution. If the same conflict appears again, Git applies your saved resolution to the file for you.
Without rerere, Git treats each conflict as new, even when you resolved identical text yesterday. This hurts when you repeat the same integration several times:
- You test-merge a topic branch, throw the result away, and merge it again later.
- You rebase a long-lived branch onto a moving main line.
- You resolve the same conflict on several branches that share a change.
rerere removes the repeated manual work. It does not change what counts as a conflict.
The record and reuse cycle
rerere works in two phases. Each phase uses a snapshot of the file.
- Record. When a merge stops with a conflict, rerere saves the conflicted file, markers included. This snapshot is the preimage. After you resolve the file and commit, rerere saves the resolved file. This snapshot is the postimage.
- Reuse. When a later merge produces a conflict that matches a saved preimage, rerere writes the matching postimage into your working tree.
The next chapters cover where these snapshots live and how Git decides that two conflicts match. For now, think of a preimage as the question and a postimage as your answer.
What you see when it reuses a resolution
On a repeated conflict, Git still reports the conflict. The difference is that the file no longer contains markers:
git merge feature
# Recorded preimage for 'settings.txt' <- first time: rerere is recording
# Automatic merge failed; fix conflicts and then commit the result.
# ...later, after resetting and repeating the same merge:
git merge feature
# Resolved 'settings.txt' using previous resolution.
# Automatic merge failed; fix conflicts and then commit the result.The line "Resolved ... using previous resolution." is the sign that rerere worked. The merge is still paused, so you must finish it yourself.
What rerere does and does not do
| rerere does | rerere does not |
|---|---|
| Save your resolution of a conflicted file | Resolve a conflict it has never seen |
| Rewrite the file in the working tree on a match | Stage the file by default (`git add` is still yours to run) |
| Work during merge, rebase, and cherry-pick | Commit anything for you |
| Keep resolutions per repository, on your machine | Share resolutions with other clones automatically |
The first row of the right column is the most important. rerere replays a past decision only when the conflict matches exactly. A new conflict needs a new human decision.
Common mistakes
- Expecting a clean merge. A reused resolution still leaves the merge unfinished. Run
git status, check the file, thengit addand commit. - Trusting a replay without looking. The saved answer was correct for the old context. Read the file before you stage it. A later chapter shows how to discard a bad resolution.
- Forgetting rerere is off by default. Nothing is recorded until you enable it. A conflict you resolved before enabling rerere cannot be replayed.
- Blaming rerere when nothing happens. If the conflicting text differs even slightly from what was recorded, there is no match and you resolve it by hand. rerere records the new resolution for next time.
Check your understanding
Exercise
You merge a branch, hit a conflict, and resolve it with rerere enabled. Later you reset the merge and repeat it. Git prints "Resolved 'settings.txt' using previous resolution." and still says the automatic merge failed. What state is the repository in, and what must you do to finish?
Show answer
The merge is still in progress. rerere only rewrote settings.txt in the working tree with the saved resolution, and it did not stage the file or create a commit. You should read the file to confirm the resolution is still right, run git add settings.txt, and then run git commit to complete the merge.