Git Rerere: Don’t Let Merge Conflicts Trouble You Twice

Git tutorial - IT technology blog
Git tutorial - IT technology blog

Teaching Git to Auto-Fix Instead of Manual Intervention

Have you ever had to fix the same conflict 5-6 times while rebase-ing a long-running feature branch? It’s incredibly frustrating. Git Rerere (Reuse Recorded Resolution) was created to solve exactly this pain point. Simply put: you only need to resolve that conflict once. Git learns your resolution and automatically applies it in the future if it encounters the same mess.

Enabling this feature is extremely simple. You just need to run a single command:

bash\ngit config --global rerere.enabled true

Once enabled, Git will silently record the state before and after you fix a file. If you only want to try it for a specific project, you don’t need to run a global config. Instead, create a directory named rr-cache in the project’s .git folder:

bash\nmkdir .git/rr-cache

Real-World Case Study: When 1 Person Refactors, 7 Others “Cry Out”

In the past, my team of 8 worked on a large project with over 200 logic files. I was tasked with refactoring the entire central configuration file structure. While I was making changes, the rest of the team was busy pushing new feature code to their own branches.

The nightmare began when I merged the refactor into master. The other 7 people encountered the same type of conflict in the config file on every single one of their commits during rebase. One guy had 50 identical conflicts. Instead of manually clicking Accept Incoming for each one, I told the team to enable Rerere. As a result, after they handled the first one, Git automatically “cruised” through the remaining 49 in seconds.

This technique is extremely powerful in two situations:

  • Continuous Rebase: You want your branch to stay up-to-date with master, but master is changing rapidly in the code area you’re working on.
  • Test Merges: You merge several branches to run tests but don’t want to commit yet. You fix conflicts, finish testing, and then abort. Next time you perform the actual merge, you won’t have to fix that mess again.

How do Preimage and Postimage Mechanisms Work?

Many people fear Git might automatically corrupt their code. In reality, its mechanism is very transparent. When a conflict occurs and Rerere is on, Git performs 3 steps:

  1. Save Preimage: Git takes a snapshot of the file containing the <<<<<<< and ======= markers.
  2. Wait for your intervention: You use your IDE to fix the code, remove conflict markers, and save.
  3. Save Postimage: As soon as you git add, Rerere saves this clean file state as the reference model.

Next time, if Git sees a conflicted file whose content matches the old preimage 100%, it will immediately pull the postimage and overwrite the conflict. You can inspect what Git remembers using the command:

bash\ngit rerere status

Or see the difference between the current fix and the previous one:

bash\ngit rerere diff

Cache Management and Advanced Options

By default, Rerere only fills in the resolved content but does not perform a git add, allowing you to double-check. If you fully trust Git and want it to auto-add for convenience, enable this option:

bash\ngit config --global rerere.autoupdate true

Cleaning Up Storage

All Rerere data resides in .git/rr-cache. To prevent this folder from bloating, Git automatically deletes unresolved preimages after 15 days. Resolved ones are kept for 60 days. You can proactively clean it up with:

bash\ngit rerere gc

Correcting Mistakes

Sometimes you resolve a conflict incorrectly, and Git quickly remembers that error. Don’t worry; you can force it to relearn from scratch by running:

bash\ngit rerere forget [filename]

Advice for Using Rerere in Project Environments

While convenient, there are a few points to note to avoid “side effects.” First, always glance over the file after Git reports an automatic resolution. Sometimes surrounding code logic has changed, making the old fix no longer 100% accurate from a business perspective. Second, Rerere is most powerful on configuration files, boilerplate, or constants files.

In summary, Git Rerere is like a smart notebook assistant. It doesn’t replace your thinking, but it eliminates meaningless, repetitive manual labor. Turn it on today—you’ll regret spending so much time manually fixing conflicts all this while!

Share: