SpunkArt
Data, text and encoding

Comparing two texts: how a diff works

A diff answers one question: what is the smallest set of line insertions and deletions that turns version A into version B? The method behind it, the longest common subsequence, is simple enough to follow by hand, and knowing it makes diff output much easier to read.

Compare two texts line by line

The longest common subsequence

A subsequence is what is left after you delete some items without reordering the rest. The longest common subsequence (LCS) of two lists is the longest list of lines that appears, in order, in both. Every line in A that is not in the LCS was deleted; every line in B that is not in it was added.

Worked example with the shopping lists above.

  • A: milk, eggs, flour, butter, sugar
  • B: milk, eggs, brown sugar, flour, butter
  • LCS: milk, eggs, flour, butter (4 lines). “sugar” and “brown sugar” are different lines, so neither is shared.
  • Result: 1 line added (“brown sugar”), 1 line removed (“sugar”), 4 unchanged.

The classic way to find the LCS fills a table with one cell per pair of lines. Each cell holds the LCS length of the remaining lines: if the two lines match, it is 1 plus the diagonal cell; if not, it is the larger of the cell below and the cell to the right. Walking the table from the top-left corner then prints the diff. The work grows with the product of the two lengths, so two 2,000-line files mean 4 million cells. That is why the tool on this page caps each side at 2,000 lines, and why real tools such as Git use faster variants like the Myers algorithm.

Reading unified diff format

Git, diff -u and code-review tools print the same example like this:

--- list-v1.txt
+++ list-v2.txt
@@ -1,5 +1,5 @@
 milk
 eggs
+brown sugar
 flour
 butter
-sugar
  • --- and +++ name the old and new files.
  • @@ -1,5 +1,5 @@ is a hunk header: this block covers 5 lines starting at line 1 in the old file, and 5 lines starting at line 1 in the new one.
  • A leading space marks an unchanged context line, + an added line, - a removed line.

Note that a changed line appears as a removal plus an addition. Diff tools do not know that “sugar” became “brown sugar”; some tools highlight the changed words inside such pairs as an extra layer.

Noise that hides real changes

ProblemSymptomFix
Windows (CRLF) vs Unix (LF) line endingsEvery line shows as changedNormalise endings; in Git, set core.autocrlf or a .gitattributes rule
Trailing spaces or tabs vs spacesLines look identical but differgit diff -w, or the ignore option above
Re-indented or re-wrapped textWhole paragraphs changeUse a word diff: git diff --word-diff
Reordered JSON keysData is equal but lines moveSort keys first, as the JSON formatting guide explains

Handy commands

  • diff -u old.txt new.txt – unified diff of two files.
  • git diff – unstaged changes; git diff --staged for staged ones.
  • git diff main..feature -- path/ – compare branches for one folder.
  • diff <(sort a.txt) <(sort b.txt) – compare two lists when order does not matter (bash).

Questions

Is there only one correct diff?

No. When several subsequences share the longest length, tools may choose differently, so two programs can show different but equally valid diffs.

Can I compare Word documents or PDFs here?

Not directly. Copy the plain text into both boxes. Formatting changes are invisible to a text diff.

Are my texts uploaded?

No. The comparison runs in your browser and nothing is saved.