Diff Algorithms Explained: Myers, Patience, Histogram
How git, IDEs, and review tools compute the differences you see every day.
Every code review and merge depends on a diff algorithm. The choice changes how readable a diff feels.
Myers Diff
The default in git, vim, and most tools. Finds the shortest edit script (minimum insertions + deletions) using O((N+M)D) time.
Strength: provably minimal edits. Weakness: can produce confusing layouts when functions move around or when blank lines are involved.
git diff # Myers by default
Patience Diff
Inspired by Bram Cohen's design. Anchors on unique lines that appear once in both files, then recurses on the segments between them.
git diff --diff-algorithm=patience
Strength: function moves and refactors look much cleaner. Weakness: slightly slower; not always minimal in raw line counts.
Histogram Diff
Improvement on patience that handles repeated lines better. The default in git for many years now in some configurations.
git diff --diff-algorithm=histogram
Strength: best for typical code with brace-heavy boilerplate.
Set Your Default
git config --global diff.algorithm histogram
Most teams adopt histogram for cleaner reviews.
Word and Character Diff
Sometimes line-level is too coarse:
git diff --word-diff
git diff --color-words
Useful for documentation and prose, distracting for code.
Three-way Merge
git merge uses a recursive three-way merge built on top of diffs. Conflicts arise when both branches changed the same hunk in incompatible ways. Choosing the diff algorithm affects how merge conflicts look.
Diffing Structured Data
For JSON, YAML, and similar, line diffs make a mess. Use:
jdfor JSONyq difffor YAMLdifft(Difftastic) for syntax-aware diffs across many languages
Difftastic is worth installing — it parses both files and diffs the AST, so reformatting changes vanish from the output.
For URL-encoded payload comparison see the [URL Decoder](https://sdk.is/url-decoder).