Learn Git Rerere - resolve a conflict once
git rerere is a "hidden" Git feature that saves you from resolving the same merge conflicts over and over again.
The name rerere stands for "reuse recorded resolution".
Its job is to remember how you resolved a specific conflict "hunk" (a specific conflicting block of code). The next time Git encounters that exact same conflict, it will automatically apply your previous resolution, saving you the manual effort.
How it Works: The rerere Workflow
rerere works by creating a small database of conflict resolutions. It automatically records two snapshots for every conflict:
- Preimage (The Conflict): When you run a command (like
git mergeorgit rebase) and a conflict occurs,git rerere(if enabled) silently saves a copy of the conflicting file, including the<<<<<<<,=======, and>>>>>>>markers. This is the "before" picture. - Postimage (The Resolution): After you manually edit the file to fix the conflict and run
git add <file>,rereresaves a copy of the resolved file. This is the "after" picture.
The next time you perform an action (e.g., rebasing another branch) and Git runs into a conflict, it checks its rerere database. If the new conflict's "preimage" exactly matches one it has on record, it automatically applies the corresponding "postimage" (the resolution) to your working file.
How to Enable git rerere
rerere is not enabled by default. The easiest way to turn it on is globally for your user:
git config --global rerere.enabled true
You can also enable it for a single repository by running the same command without the --global flag inside that repo.
Once enabled, it works in the background. You don't need to run any special commands for it to start recording resolutions.
Common Use Cases (When is it useful?)
rerere is most valuable in workflows where you expect to see the same conflicts repeatedly:
- Long-Lived Feature Branches: Imagine you have a
featurebranch that you've been working on for weeks. You regularly mergemaininto yourfeaturebranch to stay up-to-date. You will likely resolve the same basic conflicts every single time you merge.rereremakes this painless after the first time. - Frequent Rebasing: If you prefer to rebase your
featurebranch on top ofmaininstead of merging,rerereis a lifesaver. Everygit rebaseessentially re-plays your commits. If a conflict was introduced early in your branch, you might have to resolve it on every subsequent commit during the rebase.rerereresolves it the first time, and then automatically applies that fix for all other commits in the rebase. - Backporting Fixes: If you are cherry-picking a bugfix from
mainto an olderreleasebranch, you might get a conflict. If you have to cherry-pick a different commit that has the same conflict,rererewill handle it.
Useful rerere Commands
While rerere is mostly automatic, you can interact with its cache using these commands:
| Command | Description |
|---|---|
git rerere status |
Shows you which files rerere is currently tracking and has recorded "preimages" for. |
git rerere diff |
Shows you the current state of the conflict resolution compared to the "preimage". This is useful if you want to see what rerere has staged automatically. |
git rerere forget <path> |
Deletes the recorded resolution for a specific file. This is useful if you made a mistake in a resolution and don't want Git to reuse it. |
git rerere clear |
(Dangerous) Empties the entire rerere database for the repository. |
git rerere gc |
Prunes old, no-longer-relevant recorded resolutions from the database. |
"Training" rerere on Past Commits
What if you only just learned about rerere but have a repository full of merge commits where you already fixed conflicts? You can "train" rerere to learn from your past resolutions.
Git includes a script for this. You can find its location by running git --exec-path, but it's often in a path like /usr/share/doc/git/contrib/.
The command to run it (from your repo's root) might look like this:
# This path may be different on your system
/usr/share/doc/git/contrib/rerere-train.sh <list-of-merge-commits>
This script will re-check the specified merge commits, identify the conflicts, and record how you resolved them, populating your rerere cache as if it had been enabled all along.
- ← Previous
Learning Python - Next →
Learn Git - Version Control