How to Reuse Git Conflict Resolutions with rerere
Enable rerere with git config rerere.enabled true, and Git records each conflict resolution you make by hand. When the same conflict comes up again, Git applies that resolution automatically. This saves you from fixing the same lines every time you redo a merge or repeatedly rebase a working branch onto main.
The test environment is Ubuntu 24.04 with Git 2.43.0. I ran the commands and included the output from before and after each operation as it appeared. Git's output is shown in English. I set the author name and email to example values (Neko <neko@example.com>), and I fixed the commit dates with environment variables. I set GIT_EDITOR=true so that git rebase --continue and similar commands would not open an editor (the message is used as is). The commands in the body omit these settings. I did not test behavior on Windows or macOS, or merges made from GUI tools or editor extensions.
- 1. Two branches that conflict
- 2. Set rerere.enabled to true
- 3. First time: the conflict, and the hand-made resolution is recorded
- 4. Second time: the recorded resolution applies to the same conflict
- 5. The same resolution is used in rebase
- 6. Use rerere.autoUpdate to also run git add automatically
- 7. Delete a wrong recorded resolution with git rerere forget
- 8. A conflict with different content is not matched
- 9. When records are deleted
Two branches that conflict
I created a topic branch from main and rewrote the description line in README.md on each branch. On topic I changed it to "share," and on main I changed it to "organize."
$ git log --oneline --graph --all
* f5bfe2e 説明に整理を足す
| * 0ab4910 説明に共有を足す
|/
* 14f5210 最初のコミット
Set rerere.enabled to true
At first, rerere.enabled is not set, and there is no .git/rr-cache folder where the records are stored.
$ git config --get rerere.enabled
$ ls .git/rr-cache
ls: cannot access '.git/rr-cache': No such file or directory
$ git config rerere.enabled true
$ git config --get rerere.enabled
true
I enabled it only for this repository. To use it in every repository, run git config --global rerere.enabled true. According to the official reference for rerere.enabled, rerere also works without this setting if .git/rr-cache exists.
First time: the conflict, and the hand-made resolution is recorded
When I merged topic into main, the usual conflict output had one extra line: Recorded preimage for 'README.md'. According to the official reference, rerere records a conflicted file in its conflicted state, with the markers still in place.
$ git merge topic
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Recorded preimage for 'README.md'
Automatic merge failed; fix conflicts and then commit the result.
$ git rerere status
README.md
$ git rerere remaining
README.md
$ cat README.md
# Cat Paw app
<<<<<<< HEAD
猫の写真を集めて整理するアプリです。
=======
猫の写真を集めて共有するアプリです。
>>>>>>> topic
$ ls .git/rr-cache
457c4d0b49208351be26b4b55227a794e3407467
$ ls .git/rr-cache/*
preimage
git rerere status lists the files for which rerere is about to record a resolution. git rerere remaining lists the files that still have conflicts rerere could not resolve. Inside .git/rr-cache, Git creates one folder per conflict, and that folder holds the conflicted state as preimage.
After I rewrote the file, I ran git rerere diff. It shows the difference between the recorded conflict and the current file.
$ printf "# 猫の手アプリ\n\n猫の写真を集めて整理・共有するアプリです。\n" > README.md
$ git rerere diff
--- a/README.md
+++ b/README.md
@@ -1,7 +1,3 @@
# Cat Paw app
-<<<<<<<
-猫の写真を集めて共有するアプリです。
-=======
-猫の写真を集めて整理するアプリです。
->>>>>>>
+猫の写真を集めて整理・共有するアプリです。
In this diff, the branch names after the markers are gone, and the order of the two lines is the reverse of the file.
When I ran git add and committed, the output changed to Recorded resolution for 'README.md'. The hand-made result was saved as postimage.
$ git add README.md
$ git commit --no-edit
Recorded resolution for 'README.md'.
[main 7b8f8f7] Merge branch 'topic'
$ ls .git/rr-cache/*
postimage
preimage
$ cat .git/rr-cache/*/postimage
# Cat Paw app
猫の写真を集めて整理・共有するアプリです。
The resolution was recorded at commit time. Nothing is printed at git add. The official reference also says that when git commit commits a merge result, it calls rerere.
Second time: the recorded resolution applies to the same conflict
I undid the merge commit with git reset --hard HEAD~1 and merged again.
$ git reset -q --hard HEAD~1
$ git log --oneline -1
f5bfe2e 説明に整理を足す
$ git merge topic
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Resolved 'README.md' using previous resolution.
Automatic merge failed; fix conflicts and then commit the result.
$ cat README.md
# Cat Paw app
猫の写真を集めて整理・共有するアプリです。
The output says Resolved 'README.md' using previous resolution., and README.md has the content I fixed the first time. There are no conflict markers.
However, from Git's point of view the conflict is not finished yet. git status still shows both modified, and trying to commit now fails with an error.
$ git status
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
$ git rerere remaining
$ git commit --no-edit
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
U README.md
$ git diff
diff --cc README.md
index 159267f,1e002c9..0000000
--- a/README.md
+++ b/README.md
@@@ -1,3 -1,3 +1,3 @@@
# Cat Paw app
- 猫の写真を集めて整理するアプリです。
-猫の写真を集めて共有するアプリです。
++猫の写真を集めて整理・共有するアプリです。
$ git add README.md
$ git commit -q --no-edit
git rerere remaining listed README.md the first time, but the second time it printed nothing. In git diff, the lines from both branches are replaced by the one line I recorded. The official git rerere reference also says that rerere does not touch the index, so you should check with git diff and then run git add yourself.
The same resolution is used in rebase
I undid the merge again, and this time I switched to topic and rebased it onto main.
$ git reset -q --hard HEAD~1
$ git switch topic
Switched to branch 'topic'
$ git rebase main
Rebasing (1/1)
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
error: could not apply 0ab4910... 説明に共有を足す
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".
Resolved 'README.md' using previous resolution.
Could not apply 0ab4910... 説明に共有を足す
$ cat README.md
# Cat Paw app
猫の写真を集めて整理・共有するアプリです。
The resolution recorded during the merge was also applied during the rebase. The official git rerere reference also says that rerere's records are used during git rebase. After that, I continued the same way as an ordinary rebase: git add, then git rebase --continue.
$ git add README.md
$ git rebase --continue
[detached HEAD 9152954] 説明に共有を足す
1 file changed, 1 insertion(+), 1 deletion(-)
Successfully rebased and updated refs/heads/topic.
$ git log --oneline --graph
* 9152954 説明に共有を足す
* f5bfe2e 説明に整理を足す
* 14f5210 最初のコミット
The official git rerere reference describes the feature as useful for a long-lived working branch, where you keep fixing the same conflicts until that branch is fully merged in. The reference's example runs a trial merge, resolves the conflicts, undoes it with git reset --hard HEAD^, and then uses the recorded resolutions when the real merge happens later.
Use rerere.autoUpdate to also run git add automatically
I undid the rebase and moved topic back. Then I set rerere.autoUpdate to true and merged again on main.
$ git reset -q --hard ORIG_HEAD
$ git log --oneline -1
0ab4910 説明に共有を足す
$ git switch -q main
$ git config rerere.autoUpdate true
$ git merge topic
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Staged 'README.md' using previous resolution.
Automatic merge failed; fix conflicts and then commit the result.
$ git status
On branch main
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: README.md
$ git diff --cached
diff --git a/README.md b/README.md
index 159267f..a89e90d 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,3 @@
# Cat Paw app
-猫の写真を集めて整理するアプリです。
+猫の写真を集めて整理・共有するアプリです。
The output changed to Staged 'README.md' using previous resolution., and git status now says "All conflicts fixed." The recorded resolution was even staged (git add) for me, so all that is left is git commit. As the official reference for rerere.autoUpdate says, the default is false.
rerere applies a previously recorded resolution without checking whether it is correct. If the first fix was wrong, the same mistake will be applied every time. Enabling rerere.autoUpdate also removes the git add step, so check the contents with git diff --cached before you commit.
Delete a wrong recorded resolution with git rerere forget
To change a recorded resolution, run git rerere forget <file> while the same conflict is still in progress. Here I stopped the merge, unset rerere.autoUpdate, and then merged again.
$ git merge --abort
$ git config --unset rerere.autoUpdate
$ git merge topic
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Resolved 'README.md' using previous resolution.
Automatic merge failed; fix conflicts and then commit the result.
$ cat README.md
# Cat Paw app
猫の写真を集めて整理・共有するアプリです。
$ git rerere forget README.md
Updated preimage for 'README.md'
Forgot resolution for 'README.md'
$ cat README.md
# Cat Paw app
猫の写真を集めて整理・共有するアプリです。
The output said Forgot resolution, so the record was removed. However, the README.md in the working tree still had the earlier resolution applied. To go back to the conflicted state and fix it again, use git checkout -m README.md to restore the content with the markers.
$ git checkout -m README.md
Recreated 1 merge conflict
$ cat README.md
# Cat Paw app
<<<<<<< ours
猫の写真を集めて整理するアプリです。
=======
猫の写真を集めて共有するアプリです。
>>>>>>> theirs
The restored markers are labeled ours and theirs, not branch names. If I fix the file from here and commit, the new resolution is recorded.
$ printf "# 猫の手アプリ\n\n猫の写真を集めて整理し、共有するアプリです。\n" > README.md
$ git add README.md
$ git commit --no-edit
Recorded resolution for 'README.md'.
[main cd0e5f2] Merge branch 'topic'
$ cat .git/rr-cache/*/postimage
# Cat Paw app
猫の写真を集めて整理し、共有するアプリです。
When I undid this merge and merged again, the new "organize and share" text was applied.
$ git reset -q --hard HEAD~1
$ git merge topic
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Resolved 'README.md' using previous resolution.
Automatic merge failed; fix conflicts and then commit the result.
$ cat README.md
# Cat Paw app
猫の写真を集めて整理し、共有するアプリです。
A conflict with different content is not matched
I stopped the merge with git merge --abort, changed the topic line to "share with family," and then merged again. The conflict was on the same line, but the content in conflict was different. So the recorded resolution was not used, and the conflict was recorded as a new one.
$ git merge --abort
$ git switch -q topic
$ printf "# 猫の手アプリ\n\n猫の写真を集めて家族と共有するアプリです。\n" > README.md
$ git commit -q -am "共有の相手を書く"
$ git switch -q main
$ git merge topic
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Recorded preimage for 'README.md'
Automatic merge failed; fix conflicts and then commit the result.
$ cat README.md
# Cat Paw app
<<<<<<< HEAD
猫の写真を集めて整理するアプリです。
=======
猫の写真を集めて家族と共有するアプリです。
>>>>>>> topic
$ ls .git/rr-cache
457c4d0b49208351be26b4b55227a794e3407467
6809b905bef68e684f17668c5ff022fb9af3a0a2
.git/rr-cache now has two folders. The rerere.enabled description also says that automatic resolution applies only to conflicts with the same content.
When records are deleted
Here I stopped the new conflict with git merge --abort without fixing it. That conflict's folder contains only preimage. The first folder, where I recorded a resolution, also has a file called thisimage, in addition to preimage and postimage.
$ ls .git/rr-cache/*
.git/rr-cache/457c4d0b49208351be26b4b55227a794e3407467:
postimage
preimage
thisimage
.git/rr-cache/6809b905bef68e684f17668c5ff022fb9af3a0a2:
preimage
$ git rerere gc
$ ls .git/rr-cache
457c4d0b49208351be26b4b55227a794e3407467
6809b905bef68e684f17668c5ff022fb9af3a0a2
git rerere gc is the command that deletes old records. Both records were just created, so both remained after the command ran. According to the official reference, by default it deletes unresolved records older than 15 days and resolved records older than 60 days. You can change these periods with gc.rerereUnresolved and gc.rerereResolved. The official git gc reference also lists rerere records among the items it cleans up.



