Rerere in Git
It’s day 5 of Blogvent, where I blog every day in December!
I learned about git rerere recently and it’s pretty neat, particularly if you are in a codebase that hits merge conflicts relatively often.
It solves the problem of when you resolve the same conflicts regularly, for example:
- Repeatedly rebasing a feature onto main (like on long-lived branches)
- Stacked PRs, if you restack a lot of dependent branches
- Multiple integration attempts, like if you try to integrate something, resolve stuff in that integration, give up, and try again later
- An agent repeatedly updates the same PR as a base changes
…and so on. rerere stands for “reuse recorded resolution” and it lets Git remember how you resolved a conflict, and can apply that resolution when it recurs in a merge or rebase!
To enable rerere, run:
git config --global rerere.enabled true
Then, in future resolutions, you’ll see this in your terminal when it runs:
Resolved 'hello-world.md' using previous resolution.
Here’s how your usage might look in context:
$ git branch first-try main
$ git branch second-try main
$ git switch -q first-try
$ git merge feature
Auto-merging hello-world.md
CONFLICT (content): Merge conflict in hello-world.md
Recorded preimage for 'hello-world.md'
Automatic merge failed; fix conflicts and then commit the result.
# pretend I resolved a merge conflict here
$ git rerere
Recorded resolution for 'hello-world.md'.
$ git add hello-world.md
$ git commit -qm "Merge feature greeting"
# now we merge the same feature branch into `second-try`, which still points to the original `main` commit
$ git switch -q second-try
$ git merge feature
Auto-merging hello-world.md
CONFLICT (content): Merge conflict in hello-world.md
Resolved 'hello-world.md' using previous resolution. # like you saw above
# then add the file and commit it!
You can also keep track of what rerere is tracking with git rerere status, and purposely have it “forget” things, too.
You can see all of the commands in the rerere documentation and can check out the getting started guide for it here!
I hope this is handy for you!