Git Diff Algorithms — myers, patience, histogram
Git Diff Algorithms — myers, patience, histogram
Thesis
git diff is not one algorithm. myers (default) is fast and good enough for most patches. patience and histogram spend more care on unique lines so moved blocks stay together in noisy files. This chapter diffs the same rewrite under all three. (The Argo CD book section on diff algorithms is about rendered app diffs — this chapter is everyday Git.)
Mental model
git -c diff.algorithm=myers|patience|histogram diff
| Algorithm | Intent |
|---|---|
| myers | Classic LCS-style; default |
| patience | Prefer unique lines as anchors |
| histogram | patience-like with histogram heuristics |
Worked examples
Git on this box: 2.47.3.
=== myers ===
diff --git a/f.txt b/f.txt
index e63706e..fc15405 100644
--- a/f.txt
+++ b/f.txt
@@ -1,10 +1,13 @@
alpha
bravo
-charlie
-delta
+CHANGED-1
+CHANGED-2
echo
foxtrot
golf
+NEW-BLOCK-A
+NEW-BLOCK-B
+NEW-BLOCK-C
hotel
india
juliet
=== patience ===
diff --git a/f.txt b/f.txt
index e63706e..fc15405 100644
--- a/f.txt
+++ b/f.txt
@@ -1,10 +1,13 @@
alpha
bravo
-charlie
-delta
+CHANGED-1
+CHANGED-2
echo
foxtrot
golf
+NEW-BLOCK-A
+NEW-BLOCK-B
+NEW-BLOCK-C
hotel
india
juliet
=== histogram ===
diff --git a/f.txt b/f.txt
index e63706e..fc15405 100644
--- a/f.txt
+++ b/f.txt
@@ -1,10 +1,13 @@
alpha
bravo
-charlie
-delta
+CHANGED-1
+CHANGED-2
echo
foxtrot
golf
+NEW-BLOCK-A
+NEW-BLOCK-B
+NEW-BLOCK-C
hotel
india
juliet
git version 2.47.3
On this small rewrite all three produced the same hunks — expected for a tidy change. Divergence shows up on large, copy-heavy files.
Trap
Changing diff.algorithm globally without telling the team. Reviews suddenly look different.
Boring rule
Leave myers for day-to-day. Switch to histogram when a review is hard to read because of moved blocks.
Try this
- Diff a file where a function moved 200 lines with all three algorithms.
- Set
diff.algorithmin a throwaway repo and re-rungit log -p. - Read
git help configunderdiff.algorithm.
Sources
git-diff(1)/git-config(1)diff.algorithm