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

How I Setup Terminal On My Mac To Make It Amazing I often get asked about my "terminal setup", and I try to throw tips but it's never enough. This answer took a long time to compile but I'm glad to share it: A terminal setup from scratch (literal white screen terminal) to multiplexing, color output, auto-completion, history manager, fonts, nerd fonts etc etc etc... I learned that the journey to a “perfect” terminal setup is a personal one, built from years of small, incremental improvements....

The Unreasonable Effectiveness of 12-Factor Apps This issue is brought to you by: Twingate: It's time to ditch your VPN. Keep private resources and internet traffic protected with Zero Trust security tools built for the modern world of work. Start securing your Kubernetes clusters today! The title of this newsletter isn’t just fluff; it points to a fundamental truth in software development. Often, the most expensive problems stem from ignoring a few simple, foundational rules. The...

Custom computer build with blue led lighting

FFmpeg Converts ANYTHING - The Trick Book I Wish I Had This issue is brought to you by: Learn backend development the smart way with boot.dev Use the code DEVOPSTOOLBOX to get 25% off your first payment for boot.dev. Get Started Today The title of this newsletter isn't an exaggeration. There's a single, free tool that can genuinely convert, create, and manipulate almost any media file you throw at it. It’s the hidden engine powering everything from YouTube, through OBS, online media...