Sub topic 9
Where rerere pays off: test merges, rebases, long-lived branches
This chapter assumes you can commit, branch, merge, and rebase, and that you have rerere switched on with git config rerere.enabled true. It covers the three situations where the same conflict tends to appear more than once, and what to watch for in each.
The pattern rerere helps with
rerere helps when Git is asked to combine the same pair of changes again. You resolved the conflict once, but the merge or rebase was thrown away, replayed, or repeated on a new commit. Without rerere, you resolve it again by hand. With rerere, Git fills in your earlier answer.
Repeated test merges
A common habit is to merge a topic branch into a scratch branch to see whether it integrates cleanly, then discard the result. Each attempt would normally repeat the same conflicts.
git switch -c trial-merge main
git merge topic # conflicts: resolve and commit once
git switch main
git branch -D trial-merge # throw the trial away
# Later, the real merge
git merge topic # rerere reuses the earlier resolutionThe first trial pays the cost. The real merge, and any later trials, reuse your answer. This is also useful when a release manager merges many topic branches into an integration branch and rebuilds it daily from scratch.
Rebasing
A rebase replays your commits one at a time on a new base. If you rebase a branch, abort, and start again, you meet the same conflicts again. rerere remembers each one.
git rebase main # stops with a conflict in app.conf
# resolve, then:
git add app.conf
git rebase --continueIf you later rebase again after main moves on, any commit that produces an identical conflict is resolved automatically. You still run git add and git rebase --continue unless rerere.autoupdate is on.
Rebase also benefits when a series of commits touches the same lines. Without rerere you may resolve the same hunk in commit 2, commit 3, and commit 4. With rerere, only the first needs your attention if the conflicting text is identical.
Long-lived branches
A long-lived branch, such as a feature or a maintenance line, is merged with the main line many times. Each time you bring in new work, some old conflicts reappear if you have been rebasing, or if you merge in several directions. Teams also hit this when the same two branches are merged into several release lines.
git switch cherry-pick-target
git cherry-pick abc1234 # conflict: resolve once
git switch other-release
git cherry-pick abc1234 # same conflict text: reusedCherry-picking one fix into several branches is the same pattern. If the surrounding code is identical in each branch, rerere reuses the resolution.
Check the result before you trust it
rerere only reuses a resolution when the conflicting text matches the recorded conflict. It does not know whether your earlier answer is right in the new context.
A quick routine after a repeated merge:
- Read the "Resolved ... using previous resolution" lines in the output.
- Run
git diffto see what rerere wrote. - Build and test.
- Stage the files and continue.
When it does not help
| Situation | Why rerere does not apply |
|---|---|
| The conflict text differs, for example a neighboring line changed | The new conflict does not match any recorded one, so you resolve it again. |
| A conflict you have never resolved in this repository | Nothing was recorded. The cache is local to the repository. |
| You want one resolution applied across different files | Matching is by conflict content, not by intent, so a changed hunk is a new conflict. |
Exercise
You merge topic into a scratch branch, resolve one conflict, and delete the scratch branch. A week later you merge topic into main and Git reports "Resolved 'app.conf' using previous resolution." Is app.conf staged, and what should you do before committing?
Show answer
It is not staged unless rerere.autoupdate is enabled. The file holds the reused resolution but still counts as conflicted. Review it with git diff, run your build and tests, then run git add app.conf and commit.
Exercise
You rebase a branch of five commits and the same hunk conflicts in commits 2, 3, and 4 with identical conflict text. How many times do you have to resolve it by hand with rerere enabled?
Show answer
Once, in commit 2. rerere records that resolution and reuses it for commits 3 and 4. You still run git add and git rebase --continue at each stop unless rerere.autoupdate is on.