Git: Improve conflict display with the zdiff3 style

Divergence diagram.

This post is an adapted extract from my book Boost Your Git DX, available now.

By default, Git presents merge conflicts like so:

My hit novel (title TBD)
========================

It was a dark and stormy night, the kind that sent shivers down your spine.
<<<<<<< HEAD
The rain dripped relentlessly from the heavens.
=======
The rain poured without end from the heavens.
>>>>>>> mum_edits
Thunder rumbled in the distance, its deep growls echoing through the night.

Between the <<<<<<< and ======= markers are the lines from the merge target. Between the ======= and >>>>>>> markers are the lines from the merge source. The labels after the arrow markers name the commit references being merged.

This merge conflict style is often sufficient to resolve a conflict. But it can be unnecessarily challenging because it’s missing something: the original lines that both sides started from. Seeing the common base that each side started from contextualizes changes, creating clarity.

Set merge.conflictStyle to zdiff3 to show that common base:

$ git config --global merge.conflictStyle zdiff3

Here’s that same merge with this style:

My hit novel (title TBD)
========================

It was a dark and stormy night, the kind that sent shivers down your spine.
<<<<<<< HEAD
The rain dripped relentlessly from the heavens.
||||||| 3960d3d
It was a drak and stormy night, the kind that sent shivers down your spine.
The rain dripped without end from the heavens.
Thndr rumbled in the distance, its deep growls echoing through the night.
=======
The rain poured without end from the heavens.
>>>>>>> mum_edits
Thunder rumbled in the distance, its deep growls echoing through the night.

The common base is now shown between the ||||||| and ======= marker lines, with the SHA of that common base commit. This is the set of lines that both sides started with before they made changes. This extra context is often useful in resolving a conflict.

The above diff shows that both sides made changes to three lines:

  1. Both fixed the typo in “dark”.
  2. Each made different edits: “without end” to “relentlessly”, and “dripped” to “poured”.
  3. Both fixed the typo in “thunder”.

Git moves equally-changed lines outside of the conflict area, because they have no conflicts to resolve. In this case, that means conflicting lines one and three have been moved out, leaving only line two, “The rain…”, to merge. For example, it could be resolved by combining the changes:

The rain poured relentlessly from the heavens.

Fin

May you resolve conflicts ever faster,

—Adam


Read my book Boost Your Git DX for many more Git lessons.


Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts:

Tags: