How a Text Diff Works (and How to Read One)

5 min read · Published 1 November 2026 · Updated 2 November 2026

A diff is the list of changes needed to turn one text into another. You see it in code review, in version control and in every document comparison tool. Understanding how it is produced explains why a diff sometimes shows more changes than you expected, and helps you set up a comparison so that it is easy to trust.

The idea: find what stayed the same

A diff tool does not search for differences directly. It looks for the longest sequence of lines (or words) that appears in both texts in the same order, called the longest common subsequence. Everything outside that shared sequence is reported as added or removed. Fewer changes in the report means a better match.

Where the common algorithm comes from

Many tools, including Git by default, use a variation of an algorithm published by Eugene Myers in 1986. It finds a shortest set of insertions and deletions and is fast when the two texts are similar, which is the usual case when comparing two versions of the same file.

Line, word and character level

  • Line level shows which whole lines were added, removed or changed. It is the most common view for source code and configuration.
  • Word level highlights exactly which words changed inside a line. This is much easier to read for prose and for long lines with one small edit.
  • Character level is useful for short strings such as identifiers and hashes, where a single character matters.

Why a diff can look strange

There is often more than one shortest set of changes. When a block of similar lines is repeated, such as several closing braces, the tool may match the wrong copy and show a confusing result even though it is technically minimal. Moved text is another limit: most diff algorithms see a moved paragraph as one deletion plus one addition, not as a move.

Noise from whitespace and line endings

Files edited on different systems may use different line endings (Windows uses CRLF, Linux and macOS use LF), and editors may add trailing spaces or change indentation. These make every line look different even when the visible text is the same. If you see an entire file marked as changed, check line endings and whitespace first.

Comparing structured data

JSON and XML can carry the same information in different layouts, for example with keys in a different order. Before comparing two JSON files, format both the same way (same indentation) and, if the order of keys is not meaningful, sort the keys. Then a real change stands out instead of being buried in layout differences.

Tips for a reliable comparison

  1. Normalise first: same line endings, same indentation, same encoding.
  2. Decide whether whitespace and letter case matter, and ignore them if they do not.
  3. Use the word-level view for prose and the line view for code.
  4. Read removed and added lines together, because a changed line appears as one removal and one addition.
  5. For very large files, compare sections or filter out generated lines such as timestamps.

A tiny worked example

Take two short sentences and compare them word by word:

Before and after

Before: the quick brown fox
After:  the quick red fox jumps

Longest common sequence: "the quick" ... "fox"
Diff:  the quick [- brown -] [+ red +] fox [+ jumps +]

How the result is built

The shared words (the, quick, fox) form the common sequence. Everything between them that is not shared becomes a change: "brown" was removed, "red" was added in its place, and "jumps" was added at the end. A line-based diff works the same way but treats each whole line as one unit, which is why a one-word edit in a long line marks the entire line as changed unless the tool also highlights the words inside it.

Choosing the right view

  • Code review: line view, with word highlighting inside changed lines.
  • Prose and documents: word view, since sentences are long and edits are small.
  • Data files: format and sort first, then use the line view.
  • Identifiers and hashes: character view, because a single character matters.

Frequently asked questions

Is a diff tool the same as git diff?
They share the same idea. Git can use several algorithms, including the default Myers algorithm and the patience and histogram variants, which can produce more readable results for code that has many similar lines.
Why does a moved paragraph show as deleted and added?
Most diff algorithms compare text in order. A paragraph moved to a different place appears once as a removal and once as an addition, because the algorithm does not try to detect moves.
How do I ignore whitespace differences?
Many tools have an option to ignore whitespace changes. If yours does not, normalise both texts first: convert line endings, remove trailing spaces and use the same indentation.
Can I compare binary files?
A text diff is not meaningful for binary formats such as images, PDFs or compiled files. Use a tool designed for that format, or compare checksums to check whether two files are identical.

Sources and further reading

Try it yourself: Text Compare runs in your browser, free and with no sign-up.

More guides