profile

ESPRESSO FRIDAYS

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

Published 19 days ago • 2 min read

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

Focus is a huge problem in modern days. These days, lots of apps and websites try to grab our attention and keep us hooked. Most of us know we should resist these distractions. But instead of making a plan, many people just wing it. I used to do that too. But I’ve had enough. Another Instagram post, YouTube short, or funny meme won’t make me feel good about my day. In fact, they: Make my workday longer. Slow down my progress, bit by bit. So, I put together some tools and rules to help me stay...

5 days ago • 3 min read

Hi friends, On July 15th, 2019, I messed up bad. Real bad. I wanted to finish a project quickly, and show a quick POC to a customer I was working with. To make a long story short, I pushed a container, to a public repo, containing admin credentials to an AWS account. I thought of myself as a senior consultant, who delivers, fast, with no mistakes. Man I managed to break that reputation. The silver-lining however, is that I learned my lesson. So deeply so, that I’ve implemented these same...

11 days ago • 3 min read

Hi Friends, Today, we’re diving into the often under-appreciated git blame. Often used to find out who last modified a line of code, git blame has several powerful features. Let’s explore some of these hidden gems, particularly focusing on the flags: -w -C -C -C -C -C -C blame -C -C -C ignores code movements across the entire history Ignoring Whitespace with the -w Flag Do you know the annoying blame results showing over an indent or a space removed / added? "-w" fixes that! git blame -w...

26 days ago • 1 min read
Share this post