Resolve Git Merge Conflicts With Magic (well, not really magic)


Hi friends,

Today we’re going to explore a lesser-known but incredibly powerful feature of Git: git rerere.

This feature is particularly useful for anyone who frequently manages branches and encounters merge conflicts.

What is git rerere?

The git rerere feature stands for “reuse recorded resolution.” It helps to automate the resolution of merge conflicts by remembering how you’ve resolved them in the past. When enabled, git rerere kicks in as soon as a conflict occurs and before you’ve started resolving it. If it recognizes a conflict that you’ve resolved before, it will automatically apply the same resolution, saving you the effort of manually resolving the same conflict repeatedly.

Before you can start taking advantage of rerere, you need to enable it in your Git configuration:

git config --global rerere.enabled true

This command activates git rerere across all your Git repositories. Alternatively, you can enable it only in a specific repository by running the command without the --global flag inside that repository.

How git rerere Benefits Rebasing

Rebasing is a common but sometimes tricky part of working with Git, especially when working on long-lived branches or in teams. Conflicts are inevitable. git rerere shines in these scenarios by remembering how conflicts were resolved during previous rebases.

This memory allows it to automatically resolve recurring conflicts that arise from applying old patches onto a newer base. This automation can reduce the complexity and time involved in resolving conflicts during a rebase.

Under The Hood

git rerere stores the resolutions in a unique way.
When a conflict is first resolved, it saves the pre-conflict and post-conflict versions of the files involved, along with a description of the conflict itself. These are stored in three separate files:

  • Preimage (pre): This file stores the state of the content just before the conflict occurred.
  • Postimage (post): This file records the state of the content after the conflict has been resolved.
  • Status: This file contains metadata about the conflict, including the unique identifiers of the conflict states.

These files are stored under the .git/rr-cache directory.

The next time git encounters an already recorded conflict of a "preimage", I'll use the recording from "postimage" to handle it.

git rerere is a robust tool that can make handling complex rebase operations much simpler. By learning to leverage it, you can significantly improve your workflow efficiency, particularly in collaborative environments where rebases and conflicts are common.

A word of caution though: `rerere` can be sometimes dangerous, and may resolve conflict in ways you didn't necessarily anticipate, in which case deleting a recording that isn't 100% relevant may be needed.
However, as long as the commit tree is kept clean, with small, traceable changes, the conflicts will also be small and easy to fix.


Thanks for reading! As always, feel free to respond to this email with feedback / suggestions / mistakes.

Happy weekend!

Whenever you’re ready, here’s how I can help you:

ESPRESSO FRIDAYS

Every once in a while I send hand picked things I've learned. Kind of like your filter to the tech internet. No spam, I promise!

Read more from ESPRESSO FRIDAYS

I've Been Using AWS Wrong for YEARS... For years, my approach to AWS felt like a battle. As a DevOps engineer and later and architect, building infra always involved a tedious process of carefully building templates and structure, reviewing, deploying, testing and iterating over and over. I’d either spend hours clicking through the console or writing endless infrastructure code, always feeling like I was one misconfiguration away from a headache. It turns out, I was making it much harder than...

You've been lied to about self hosting... This issue is brought to you by: Auth0, my auth provider for the last 6 years. Join their free virtual dev_day on June 18th to learn how to secure AI agents and applications. Save your free spot That title might sound a bit aggressive, but this isn't about hating on hosting platforms. It's about loving the freedom, control, and cost-savings that come from owning your deployment process, without giving up the slick, easy experience we all love. And...

How DHH Solved Deploying to Production (with Open Source) Ever felt depressed by the sheer complexity of getting your application live and serving users? You’re not alone. But what if deploying to production, even (or especially) across multiple servers, could be straightforward and more importantly, free? That’s the reality DHH, the creator of Ruby on Rails and CTO of Basecamp & HEY, wanted to create, and he delivered with an open source tool called Kamal. DHH’s approach to technology always...