guide/diff-algorithms.md

Myers, Patience, Histogram — Comparing Diff Algorithms

How each value of git diff's --diff-algorithm option (myers, minimal, patience, histogram) works and why their results differ, with examples.

Last updated: 2026-09-23

Comparing the same two files can give different results depending on the diff algorithm. All of them produce a "correct" diff, but they choose differently which lines to pair as equal, and that changes how easy the result is to read. git offers four through the --diff-algorithm option.

ValueDescription in the git docsCharacteristics
myers (default)The basic greedy diff algorithm; currently the defaultFast and usually close to minimal
minimalSpends extra time to make sure the smallest possible diff is producedAlways minimal; can be slow on large inputs
patienceUses the patience diff algorithmAnchors on unique lines
histogramExtends patience to support low-occurrence common elementsAnchors on rare lines

There are also short forms such as git diff --patience and git diff --histogram, and you can change the default with git config diff.algorithm histogram.

Myers: the default that finds a minimal edit

The Myers algorithm finds a path that minimizes the number of deletions and additions (see How diff works for the principle). The trouble comes when there are several choices for which lines to treat as equal. Code is full of very common lines such as }, {, blank lines, and return, so the algorithm can pair semantically unrelated lines and still have the same minimal number of edits.

The same change, two results

Suppose you insert a new function h between functions f and g, as below. Both of the following diffs have the same number of edits: 4 added lines.

@@ -1,6 +1,10 @@ int f() {     return 1; }++int h() {+    return 3;+}  int g() {     return 2;
@@ -1,5 +1,9 @@ int f() {     return 1;+}++int h() {+    return 3; }  int g() {

The first shows the new function as a whole, but the second treats the closing brace of the existing function f as the brace of the new function, so the addition is cut awkwardly. Shapes like this arise from the search order, and recent versions of git fix many of them with a heuristic that shifts the boundaries of changed blocks to more readable positions based on indentation (--indent-heuristic, on by default).

Patience: match unique lines first

Patience diff, proposed by Bram Cohen, first looks for lines that appear exactly once in each file. Unique lines, such as a function declaration int h() {, are certain to match. From these unique lines it picks the longest list that is in the same order on both sides (the longest increasing subsequence) as anchors, and then compares only the small regions between anchors again.

Histogram: prefer rare lines

The histogram algorithm was developed in JGit (a git implementation written in Java) and later added to git. Whereas patience looks only at "lines that occur exactly once", histogram counts how often each line occurs and picks the least frequent lines as anchors. So even when there are no unique lines, it can anchor on relatively rare ones. The git documentation describes it as extending "the patience algorithm to support low-occurrence common elements". In practice, like patience, it often produces results that preserve the structure of code well.

Which one to use

Whichever algorithm you use, applying the result reconstructs the same file. Only the shape a person reads changes.

In this tool

This site compares lines with a Myers-family algorithm, then pairs changed lines as "modified" based on how similar their contents are and compares inside those lines again. So even if the line-level result is split a little awkwardly, you can see right away which words changed. Put the example above into the Text Diff Checker and compare how it looks in unified and split views. How to read the result is covered in How to read a unified diff.

Go to the text diff tool