Guide Developer Tools 6 min read

The shortest diff is not always the one that shows what you did

Git's default diff is not always the shortest one: across 1,000 commits of the open-source Express framework it ran longer than git diff --minimal on 8 of 1,104 file changes that both added and removed lines. The shortest diff can also misdescribe an edit, turning a moved function into two functions rewritten into each other.

Kenji Tanaka
Developer Tools & Cloud Analyst
Published 15 Nov 2026, 9:14 AM (SGT)
Share:
A red pen resting on a typed page marked up with handwritten corrections. A red pen resting on a typed page marked up with handwritten corrections. Photo by annekarakash on Pixabay
Advertisement

Git's default diff does not always find the fewest changed lines. We ran the last 1,000 non-merge commits of Express, the open-source Node.js web framework, through Git 2.50.1 with four algorithms. On 8 of the 1,104 file changes that both added and removed lines, the default came out longer than git diff --minimal, by 74 lines in 16,286. Patience and histogram ran longer on 13 and 10. None was ever shorter. And the shortest answer is not always the faithful one: move a function above its neighbour and the minimal script rewrites each into the other.

Every edited line is struck through and written in again

Hunt and McIlroy's Bell Labs Computing Science Technical Report #41, of July 1976, says the central algorithm of Unix diff solves the longest common subsequence problem: find the largest set of lines both texts share in the same order, and report the rest. Eugene Myers' 1986 paper in Algorithmica gave an O(ND) method, where D is, in its abstract, "the size of the minimum edit script", with a refinement that needs only linear space.

In the unified format Git prints, an edited line appears as a removal followed by an addition, so on our counts one edited line costs two changed lines.

Git's default strikes out a few lines it never needed to

Our range runs from Express commit 43fca52d28a4 of 2 November 2026 back to 935f05bc8431 of 13 January 2015. We excluded JSON, minified JavaScript, SVG and lock files, ran git log -p -U3 --no-renames per algorithm with user configuration switched off, and kept the 1,104 of 2,060 file changes that both added and removed lines.

AlgorithmChanged linesFile changes longer than minimalExtra lines
--minimal16,286reference0
myers (default)16,360874
patience16,46213176
histogram16,40410118

Summed per commit, the default and histogram disagreed on 15 of the 1,000 commits, or 1.5%. Nugroho, Hata and Matsumoto measured this before us, finding 1.7% to 8.2% across 14 Java projects in a study posted as arXiv 1902.02467 and published in Empirical Software Engineering, volume 25.

The rate depends on the edits. In this site's own repository, whose last 1,000 non-merge commits, from 17 October to 9 November 2026, are large batch commits, many machine-assisted, the default ran longer on 164 of 2,579 such file changes. Patience ran longer on 241 and histogram on 282, the reverse of their order on Express, so we would not rank the two in general.

Frequent lines are the ones Git's shortcut crosses out early

Git's diff options page calls the default "the basic greedy diff algorithm" and says --minimal will "Spend extra time to make sure the smallest possible diff is produced." That label does not locate the extra lines. The xdiff source at v2.50.1 takes two shortcuts that --minimal switches off. One can set aside a line that recurs often in the other file, at least roughly the square root of its own file's line count, capped at 1,024, when it sits in a run of such lines. The other cuts a costly search short, in the source's words "to return a suboptimal point".

Advertisement

The first shortcut does not wait for a big diff. In Express commit 60e2008d of 19 June 2015, lib/router/layer.js comes out at 14 changed lines under minimal, patience and histogram, and 16 under the default, which removes a blank line and adds one back. The indent heuristic is not the cause. When we made the 29 blank lines outside that hunk unique, the default dropped to 14. That is consistent with the frequent-line shortcut; we did not trace Git's code path. The GNU diffutils manual's Performance page describes GNU diff's own default as "near-minimal"; we did not run GNU diff.

A moved function gets transposed into its neighbour by the shortest script

We wrote 13 pairs of test files as an illustration rather than a sample. In one, perimeter() moves above area(). Minimal and the default both give 8 changed lines: they turn each function's signature and return line into the other's and keep the shared braces and guard as unchanged. Patience and histogram give 16, with area() deleted whole and added back below perimeter(), which is how a person would describe the edit.

Bram Cohen, who wrote patience diff, explained in a post of 30 March 2010 that it first matches lines that "occur exactly once on both sides", then fills in the gaps. Git describes histogram as extending patience to "support low-occurrence common elements". In our other 12 pairs, all four algorithms gave identical counts.

This page marks up the shortest script, matching --minimal file for file

Our plain text diff, as revised on 9 November 2026, computes the exact shortest edit script. In the unified diff it copies out, its changed-line counts equal git diff --minimal on all 1,104 Express file changes and all 2,579 from our repository. Hunk counts differ on 3 and 76 of them, because Git slides change groups and the page does not. To re-run the Express case, paste lib/router/layer.js from the parent of 60e2008d as the original and from 60e2008d as the changed text: the page reports 4 added and 10 removed, where Git's default gives 5 and 11.

Past 4,000 added or removed lines, about 2,000 edited ones, it stops guaranteeing the shortest script and says the result may not be the smallest. Until 9 November 2026 that fallback marked every line of a repetitive stretch as changed: 20,000 lines drawn from 8 values, 2,226 of them then changed, came out at 40,000 changed lines, where git diff --minimal gives 4,328. The revised page lines the texts up on lines unique to both, diffs each stretch with a step-limited search, and gives 4,328.

The indent heuristic moves the caret and leaves the count alone

Git 2.11.0 added an indent heuristic as an option and Git 2.14.0 made it the default; the options page says it "shifts diff hunk boundaries to make patches easier to read." In our test pair that inserts one <li> block among identical ones, Git's default shows the new block whole. Apple's diff -u, Git with --no-indent-heuristic and our page start the insertion one line later. Both are correct patches with the same count.

Re-indented blocks come back in red ink from top to bottom

Two of our test pairs cost as much as a rewrite under every algorithm. Adding a parameter to a Python function and wrapping seven of its lines in a new if produced 17 changed lines, though only the signature and the new if differed beyond their indentation. Rewrapping a paragraph from 72 to 60 columns produced 15 with not one word altered.

We ran Git's own remedies on the same pairs. Its -w option, which will "Ignore whitespace when comparing lines", cut the re-indent to 3 changed lines: the old signature, the new one and the if. On the rewrapped paragraph, git diff --word-diff, splitting words at whitespace, marked no word at all.

Advertisement
Kenji Tanaka
Developer Tools & Cloud Analyst

Kenji Tanaka covers developer tools, cloud platforms, DevOps, CI/CD, and software supply-chain topics for RECATOOLS.

View author profile → · Editorial policy

About this byline Kenji Tanaka is a RECATOOLS editorial persona for developer tools, cloud, DevOps, and software supply-chain coverage. Articles are produced and reviewed under RECATOOLS editorial supervision.

Corrections policy

Advertisement