Sub topic 6
Walkthrough: record a resolution, then reuse it
This chapter assumes you can commit, branch, and merge, and that you can resolve conflict markers by hand. It also assumes rerere is already enabled, as covered in the previous chapter. Here you create one conflict, resolve it once, undo the merge, and watch Git resolve the same conflict for you.
The cycle in four steps
rerere works in two phases. It records when a conflict appears and when you finish resolving it. It reuses the record when the same conflict appears again.
Build the conflict
Run this in a scratch directory. Both branches change the same line of settings.txt in different ways, so Git cannot merge them automatically.
git init rerere-demo && cd rerere-demo
git config rerere.enabled true
printf 'color=red\n' > settings.txt
git add settings.txt
git commit -m "base"
git switch -c feature
printf 'color=blue\n' > settings.txt
git commit -am "feature: blue"
git switch -
printf 'color=green\n' > settings.txt
git commit -am "main line: green"Record the first resolution
Start the merge. It conflicts, as any merge would. The new part is an extra line in the output.
git merge featureGit prints Recorded preimage for 'settings.txt'. The preimage is a copy of the file with its conflict markers. Now resolve the file as you normally would, and commit.
printf 'color=teal\n' > settings.txt
git add settings.txt
git commit -m "Merge feature"On commit, Git prints Recorded resolution for 'settings.txt'. That saves the postimage, which is the file after your resolution. Together, the preimage and postimage form one record.
Repeat the merge
Throw the merge commit away and merge again.
git reset --hard HEAD^
git merge feature
cat settings.txtGit still reports a conflict, and the merge still stops. But it also prints Resolved 'settings.txt' using previous resolution. The file now contains color=teal, with no conflict markers. Finish the merge:
git add settings.txt
git commit -m "Merge feature"What to check each time
After a reused resolution, read the file before you stage it. rerere matches on the conflict's content, not on your intent, so a resolution that was right once can be wrong in a changed context.
| Message from Git | What it means |
|---|---|
| `Recorded preimage for 'file'` | rerere saw a new conflict and saved the marked-up file |
| `Recorded resolution for 'file'` | You committed, so rerere saved your resolved version |
| `Resolved 'file' using previous resolution.` | rerere found a matching record and rewrote the file |
Common mistakes
- No record appears. If
Recorded preimagenever prints, rerere is not enabled in this repository. Checkgit config rerere.enabled. - No resolution is saved. The resolution is recorded when you commit, or when a rebase or cherry-pick continues. If you abort the merge, only the preimage remains.
- The conflict looks the same, but nothing is reused. The conflicting hunks must match exactly, as the next chapters explain. If either side changed the lines, rerere treats it as a new conflict.
Exercise
After the second git merge feature in this walkthrough, git status still shows settings.txt as unmerged even though the file holds color=teal. Why, and what do you run to complete the merge?
Show answer
rerere only rewrites the working-tree file. It does not stage it, so Git still treats the path as conflicted. Run git add settings.txt after checking the contents, then git commit to finish the merge.
Exercise
You abort a conflicted merge with git merge --abort before resolving anything. Will a later identical merge be resolved automatically? Why or why not?
Show answer
No. rerere saved a preimage when the conflict appeared, but a resolution is recorded only after you resolve the file and commit. With no postimage, there is nothing to reuse.