Sub topic 10
Undoing a wrong recorded resolution
This chapter assumes you have used rerere to record and reuse a resolution, as in the earlier walkthrough. It also assumes you can resolve conflict markers by hand and know the basics of merge and rebase.
Why a bad resolution keeps coming back
rerere does not judge a resolution. It saves whatever file you committed after a conflict, and it applies that file every time the same conflict appears. If you resolved a conflict badly, the mistake comes back on every later merge or rebase that hits the same conflict.
Nothing in the working tree says which resolution is the bad one. Git only prints a line such as Resolved 'settings.txt' using previous resolution. You have to notice that the result is wrong.
The forget command
git rerere forget <pathspec> deletes the saved resolution for the conflict that is currently in that path. The next resolution you make is recorded in its place.
The command works from the conflict state in the index. The index must still hold the unmerged entries for the file. That causes two situations:
- If rerere filled in the file but you have not run
git add, the entries are still unmerged. You can runforgetright away. - If you already ran
git add, orrerere.autoupdatestaged the file for you, the conflict is gone from the index. You must recreate it first.
Walkthrough: replace a resolution
Suppose the earlier demo recorded color=teal for settings.txt, and you now decide it should be color=blue. Start the merge again:
git merge feature
# Resolved 'settings.txt' using previous resolution.
cat settings.txt # color=teal, which is the wrong result
# Recreate the conflict markers, even if the file was staged
git checkout --merge -- settings.txt
# Drop the recorded resolution for this conflict
git rerere forget settings.txt
# Resolve it the way you want, then stage and commit
printf 'color=blue\n' > settings.txt
git add settings.txt
git commit -m "Merge feature"When you commit, rerere records color=blue as the new postimage for that conflict. The next time the conflict appears, Git applies blue.
When the bad merge is already committed
forget changes only what rerere has saved. It does not edit commits you have already made. If you committed the wrong merge, you have to redo it:
- Undo the merge commit.
git reset --hard HEAD^does this for a merge you have not pushed. It discards uncommitted work, so checkgit statusfirst. - Run the merge again. rerere reuses the bad resolution and fills in the file.
- Run
git checkout --merge -- <path>, thengit rerere forget <path>. - Resolve the file correctly, then stage and commit.
If you have already pushed the merge, do not rewrite history on a shared branch. Fix the result with a new commit instead. You can still run forget so the bad resolution is not reused in future merges.
Checking that it worked
Use the commands from the inspection chapter to confirm the state:
git rerere status # the file should be listed while the conflict is open
git rerere diff # shows how your current file differs from the recorded conflict
ls .git/rr-cache/* # the conflict directory should hold the new postimage after you commitAfter forget, the conflict's directory in .git/rr-cache keeps the preimage but no longer has the old postimage. Once you commit, the new postimage appears there.
Summary
- rerere reuses a resolution even when it is wrong.
git checkout --merge -- <path>recreates the conflict markers.git rerere forget <path>removes the saved resolution for that conflict.- Resolve the file again and commit to record the replacement.
- Already committed merges must be redone or fixed with a new commit.
Exercise
You run a rebase and rerere reuses a resolution you now know is wrong. The file is already staged, because rerere.autoupdate is on. You run git rerere forget settings.txt and get an error saying no resolution is remembered. What do you do first, and why?
Show answer
Run git checkout --merge -- settings.txt. Staging removed the file's unmerged entries from the index, and forget works only on a file that is still unmerged. This command recreates the conflict, so git rerere forget settings.txt can then remove the saved resolution. After that, resolve the file correctly, stage it, and continue the rebase so the new resolution is recorded.