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

He Made $64K Searching GitHub With A GENIUS Trick (using open source only) This issue is brought to you by: TestSprite is the Easiest AI Agent for Software Testing Ensure End-to-End Confidence in Your Software Quality. LEARN MORE This, is the story of how one individual, "Mr. B," leveraged a deep understanding of Git's less-explored features to uncover secrets in public repositories, earning over $64,000 💰. His "genius trick" wasn't about finding new tools, but about using existing Git...

Google's Git Killer Is INSANELY Better (and it's open source) You saw the title. Bold claim, right? "Insanely better"? Than Git? Git is the foundation of modern software development. It started back around 20 years ago, when Linus tried to build his first Linux kernel, and had enough of SVN. So Linus being Linus, he just went ahead and built his own. But what if the way we've always done version control isn't the only way, or even the best way anymore? That's the core idea behind Jujutsu (jj)...

You Need To Learn Docker Swarm! Ever felt like you're overcomplicating your container deployments? You might be. Today, we're diving into a Docker orchestrator that's likely already on your machine (run `docker service` for a second will ya?), but you're probably overlooking: Docker Swarm. The Underdog Orchestrator For years (for me, the past 11 years to be exact), the path has seemed to be either simple Docker Compose or the more, WAY MORE complex, Kubernetes. Compose is great for local...