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.
On this page ⌄
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:
| Algorithm | Insertions | Deletions |
|---|---|---|
| myers (default) | 7 | 20 |
| minimal | 7 | 20 |
| patience | 10 | 23 |
| histogram | 10 | 23 |
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 reportedonevsone(the leading indentation counts) and ignored the trailing spaces.--ignore-space-at-eolalso reportedonevsoneand 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
zdiff3for conflict markers and--ignore-cr-at-eol(or-wwith 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
Comparing JSON Structurally: What Counts as Equal (Tested in JS and Python)
Key order, 1 vs 1.0, null vs missing, array order and big integers decide whether two JSON documents are 'equal'. Tested results for JS, Python and the Xerobit diff tool.
Git Diff Explained: Output, Branches, Staged and Two-Dot vs Three-Dot
How to use git diff: what it compares by default, staged changes, two commits, between branches with two or three dots, two files, whitespace options and exit codes.
Related tools
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.
Hours Calculator
Add start, end and break times for each day to see daily hours, weekly totals and overtime instantly.
Character Counter
Count characters, words, sentences, lines and UTF-8 bytes with live SEO and social media limit tracking.
Keyboard Test
Press any key to see it light up and find dead, stuck or double-typing keys, with rollover checks for full size, TKL and 75% layouts.
Written by Mian Ali Khalid. More guides in Dev Productivity.