Xerobit
Dev Productivity

Why git diff Shows the Wrong Lines: Myers vs Patience vs Histogram

Git's default Myers diff can show a moved function as one rewritten into another. A reproducible file pair, the histogram fix, zdiff3 conflicts and CRLF noise on Git 2.53.

MA Mian Ali Khalid Updated Nov 2, 2026 9 min read
Free tool
Text Diff
Compare two text blocks line-by-line or word-by-word. Unified and split view. Shows added, removed, and changed segments with full color coding.
Open tool →
On this page ⌄
  1. A pair of files where the default misleads
  2. Switch it on
  3. Background: what Myers does
  4. Conflict markers: merge, diff3 and zdiff3
  5. Whitespace and line-ending noise
  6. Which to use
  7. Tested

Git's default diff is correct but not always readable. When you reorder or move blocks that look alike (functions with the same skeleton, test cases, config sections), the default algorithm can report "this function was rewritten into that one" instead of "this function moved". The result is a diff that is shorter than the honest one and harder to review. Git ships three alternatives, and one config line switches them on. Everything below was run on Git 2.53.0 on 2026-11-02.

To compare two pasted texts without Git, the Text Diff tool does line-level comparison in the browser. For the output format itself (headers, hunks, +/-), see reading git diff output.

A pair of files where the default misleads

Three functions with identical structure. The change: save is deleted and reset moves above emit. Generate the files (so you can reproduce this):

def fn(name, k):
    L = [f"function {name}(opts) {{"]
    for i in range(k):
        L += [f"  if (opts.{name}{i}) {{", f"    run{name.title()}{i}(opts);", "  }"]
    return L + ["  return opts;", "}", ""]

old = fn('emit', 3) + fn('reset', 2) + fn('save', 3)
new = fn('reset', 2) + fn('emit', 3)
open('p_old.js', 'w', newline='\n').write("\n".join(old) + "\n")
open('p_new.js', 'w', newline='\n').write("\n".join(new) + "\n")

Default (Myers):

$ git diff --no-index --diff-algorithm=myers p_old.js p_new.js
@@ -1,16 +1,3 @@
-function emit(opts) {
-  if (opts.emit0) {
-    runEmit0(opts);
-  }
-  if (opts.emit1) {
-    runEmit1(opts);
-  }
-  if (opts.emit2) {
-    runEmit2(opts);
-  }
-  return opts;
-}
-
 function reset(opts) {
   if (opts.reset0) {
     runReset0(opts);
@@ -21,15 +8,15 @@ function reset(opts) {
   return opts;
 }
 
-function save(opts) {
-  if (opts.save0) {
-    runSave0(opts);
+function emit(opts) {
+  if (opts.emit0) {
+    runEmit0(opts);
   }
-  if (opts.save1) {
-    runSave1(opts);
+  if (opts.emit1) {
+    runEmit1(opts);
   }
-  if (opts.save2) {
-    runSave2(opts);
+  if (opts.emit2) {
+    runEmit2(opts);
   }
   return opts;
 }

Read that as a reviewer: emit was deleted, and save was edited line by line into emit. Neither is true. Histogram (patience printed exactly the same output):

$ git diff --no-index --diff-algorithm=histogram p_old.js p_new.js
@@ -1,3 +1,13 @@
+function reset(opts) {
+  if (opts.reset0) {
+    runReset0(opts);
+  }
+  if (opts.reset1) {
+    runReset1(opts);
+  }
+  return opts;
+}
+
 function emit(opts) {
   if (opts.emit0) {
     runEmit0(opts);
@@ -11,26 +21,3 @@ function emit(opts) {
   return opts;
 }
 
-function reset(opts) {
-  if (opts.reset0) {
-    runReset0(opts);
-  }
-  if (opts.reset1) {
-    runReset1(opts);
-  }
-  return opts;
-}
-
-function save(opts) {
-  if (opts.save0) {
-    runSave0(opts);
-  }
-  if (opts.save1) {
-    runSave1(opts);
-  }
-  if (opts.save2) {
-    runSave2(opts);
-  }
-  return opts;
-}

Now the story is whole blocks: reset was inserted at the top and removed from the middle (a move), save was deleted, emit stayed put. The surprising number is the size:

AlgorithmInsertionsDeletions
myers (default)720
minimal720
patience1023
histogram1023

The misleading diff is the smaller one. Myers (and minimal) find the shortest edit script by matching as many lines as possible, and in code the cheapest matches are boilerplate: }, }, return opts;, blank lines. The Git documentation describes minimal as spending extra time to make sure the smallest possible diff is produced, and describes histogram as extending patience to "support low-occurrence common elements". Patience and histogram anchor on lines that are rare (a unique function reset(opts) { line) instead of on lines that appear everywhere, so they cost a few extra changed lines and read like what you actually did. This pair was found by random search over generated reorderings, and most of the random cases tried printed identical output under both algorithms, so treat the example as an illustration of the failure mode, not a rate. Not measured: how often it matters in a real repository.

Switch it on

Per command:

git diff --diff-algorithm=histogram
git log -p --diff-algorithm=histogram

Permanently:

git config --global diff.algorithm histogram

Set in a scratch repo, git config --get diff.algorithm printed histogram, and git diff produced the histogram output above. To go back for one command, use --diff-algorithm=default; the docs note that this is the only way to override a configured non-default algorithm. Not tested: whether the setting changes git merge results, so no claim is made about merges.

Background: what Myers does

Eugene Myers' 1986 paper is titled "An O(ND) Difference Algorithm and Its Variations". It models the two files as a grid: the x-axis is the old lines, the y-axis the new ones. Moving right deletes an old line, moving down inserts a new one, and a diagonal step is free when the two lines are equal. The shortest edit script is the path from the top-left to the bottom-right corner with the most diagonals. The algorithm explores paths in rounds of D edits, so it is fast when the files are similar (D small), which is why it suits version control.

What matters for the example above: "most diagonals" counts every equal line as equally valuable. A } is worth as much as a unique function signature. That is a property of the objective, not a bug in the implementation.

Conflict markers: merge, diff3 and zdiff3

When two branches change the same lines, Git writes markers. Same conflict, three styles, from a scratch repo (base: TAX = 0.2, sum = 0, return sum; each branch edits all three lines, identically on the first and third):

# merge.conflictstyle = merge (default)
const TAX = 0.25;
<<<<<<< HEAD
let sum = 50;
=======
let sum = 100;
>>>>>>> feature
return sum + TAX;
# diff3
<<<<<<< HEAD
const TAX = 0.25;
let sum = 50;
return sum + TAX;
||||||| a50e823
const TAX = 0.2;
let sum = 0;
return sum;
=======
const TAX = 0.25;
let sum = 100;
return sum + TAX;
>>>>>>> feature
# zdiff3
const TAX = 0.25;
<<<<<<< HEAD
let sum = 50;
||||||| a50e823
const TAX = 0.2;
let sum = 0;
return sum;
=======
let sum = 100;
>>>>>>> feature
return sum + TAX;

Between <<<<<<< and ======= is your side; between ======= and >>>>>>> is theirs. diff3 adds the common ancestor between ||||||| and =======, which lets you see what each side changed from, not just where they differ. Its drawback is that matching edge lines (TAX = 0.25 and return sum + TAX, identical on both sides) stay inside the conflict. zdiff3 pulls those matching lines out, as the Git docs describe: it removes matching lines on the two sides from the conflict region when they are near either end. You get the ancestor and a small region. Set it with git config --global merge.conflictStyle zdiff3. This was run on Git 2.53; zdiff3 is reported to have arrived in Git 2.35, but older versions were not checked, so on an older Git use diff3.

Whitespace and line-ending noise

A diff that shows every line changed usually means line endings or indentation. File l.txt has LF endings; c.txt is the same text with CRLF and three changed to three CHANGED:

$ git diff --no-index --stat l.txt c.txt
 1 file changed, 3 insertions(+), 3 deletions(-)

$ git diff --no-index --ignore-cr-at-eol l.txt c.txt
@@ -1,3 +1,3 @@
 one
 two
-three
+three CHANGED

Three changed lines become one. For indentation and trailing spaces, with ws.txt containing one and two against the plain version:

  • -w (ignore all whitespace) produced an empty diff.
  • -b (ignore changes in the amount of whitespace) still reported one vs one (the leading indentation counts) and ignored the trailing spaces.
  • --ignore-space-at-eol also reported one vs one and ignored the trailing spaces.

On Windows, Git may print "LF will be replaced by CRLF" warnings when core.autocrlf is on; that is a hint the same noise will appear in your real diffs. Fix the cause with .gitattributes (* text=auto) rather than hiding it forever with -w, because -w hides real changes too, such as Python indentation or YAML nesting.

Which to use

  • Use histogram as your global default if you review code diffs. It suits source code well.
  • Use patience if you want the original anchor-on-unique-lines behaviour; it matched histogram here.
  • Keep myers or use minimal when you want the smallest patch, for example to ship a patch over a constrained channel.
  • Add zdiff3 for conflict markers and --ignore-cr-at-eol (or -w with care) when a diff is mostly line-ending noise.

For comparing structured data instead of lines, a line diff is the wrong tool; see comparing JSON structurally or paste both documents into the JSON Diff.

Tested

Git 2.53.0.windows.1, Python 3.11.15, Windows 11, 2026-11-02. The file pair was generated by search: a script produced random reorderings of generated functions and kept the first pair where Myers produced more change blocks than histogram, then the pair was reduced to the three-function case shown. The diffs, numstat table, git config switch, the three conflict styles (real git merge of two branches) and the whitespace options are the literal output of those runs. Algorithm descriptions, --ignore-cr-at-eol and the zdiff3 description are from git-scm.com documentation fetched on the same date. The Myers paper title is not verified against the Algorithmica publication.

Keep reading

Related tools

Written by Mian Ali Khalid. More guides in Dev Productivity.