What a diff actually computes
A comparison tool finds the longest common subsequence of lines between two texts, then marks everything outside it as an addition or a removal. The output is the shortest edit script: the fewest changes that turn one text into the other.
Line-based vs character-based
- Line-based — compares whole lines. Fast, stable and how Git works. A one-character change marks the entire line as changed.
- Character-based — drills into changed lines to highlight the exact characters. Better for editing prose, translations or long configuration values.
Reading the markers
- the old line (removed)
+ the new line (added)
unchanged line
A replacement appears as one removal plus one addition. Do not read them as two separate events when reviewing: the old line did not disappear and a new one appear elsewhere, it was edited in place.
Phantom differences
Most "the texts are different but look identical" cases come from invisible characters:
- Trailing spaces at the end of a line
- Tabs vs spaces used for the same indentation
- Line endings — Windows CRLF versus Unix LF. Every line appears changed when the two files use different conventions.
- Non-breaking spaces (
U+00A0) pasted from a web page or word processor - Unicode normalisation — the same accented character can be stored as one code point or as a base letter plus a combining mark
Where diffing pays off
- Reviewing a configuration change before restarting a service
- Confirming that a translation kept every placeholder such as
{name} - Spotting what changed between two versions of a contract or policy
- Checking that a "small" code edit really is small
A quick workflow
Paste both versions, force a line-based comparison first to see the shape of the change, then switch to character-level detail only for the lines that differ. If everything shows as changed, suspect line endings before assuming a real difference.
What the algorithm is really doing
Comparing two texts for the smallest set of changes is the longest common subsequence problem. A diff tool builds a table of matches between the two sequences, then walks back through it to produce the shortest edit script. The classic implementation used by Git is Myers’ algorithm, which runs in time proportional to the number of lines plus the number of differences — fast when the files are similar, slower when they differ almost everywhere. That is also why a moved block appears as a deletion plus an insertion: the algorithm reports the cheapest edit script, not the intention behind the edit. Some tools add a move detection step on top, but the underlying output is still additions and removals.
Unified diff, hunks and context
The format Git prints is called a unified diff. Each changed region is a hunk, introduced by a header such as @@ -12,7 +12,9 @@, which says that the hunk starts at line 12 in the original and covers 7 lines, and starts at line 12 in the new version with 9 lines. Lines beginning with a minus were removed, lines with a plus were added, and unmarked lines are context kept so a reader can see where the change sits. Three lines of context is the default in Git; a larger number helps when reviewing a change inside a long function. Side-by-side views show the same hunks in two columns, which is easier for prose but harder to copy into a patch.
Choosing the comparison granularity
| Mode | Best for | Trade-off |
|---|---|---|
| Line | Code, configuration, logs | A small edit marks the whole line as changed |
| Word | Prose, contracts, translations | Less noise, but punctuation changes can look odd |
| Character | Identifiers, long values, encoded strings | Most precise, most visual noise |
Reading a diff is easier when the mode matches the content. Comparing two paragraphs line by line, where each paragraph is stored as one long line, marks the entire paragraph as replaced and hides the single word that actually changed.
Normalise before you compare
Many comparisons should begin with a deliberate clean-up pass. Trim trailing whitespace, convert line endings to one convention, and collapse runs of spaces if the text is prose rather than code. Decide whether case matters, and apply Unicode normalisation so an accented character stored as one code point matches the same character built from a base letter and a combining mark. Remove a byte-order mark if one is present. For structured text, parse both sides and compare the data rather than the serialisation, because key order, indentation and number formatting are not meaningful differences.
Invisible characters that create phantom changes
- CRLF vs LF — the single most common cause of “every line changed”; convert to one style first.
- Trailing whitespace — invisible in the editor but a real difference to the tool.
- Tabs and spaces — identical indentation can be produced by different characters.
- Non-breaking spaces — pasted from web pages and word processors as
U+00A0. - Zero-width characters —
U+200Band similar marks occupy no visible space. - BOM — invisible at the start of a UTF-8 file but present in the byte stream.
- Smart quotes — curly quotes from a word processor differ from the straight quotes in the source.
- Full-width and half-width forms — Chinese and Japanese text may contain characters that look Latin but are not.
- A final newline — one file ends with a line break and the other does not.
When two texts look identical but the diff insists they differ, turn on the option that shows whitespace and character codes. Seeing the actual bytes resolves the mystery faster than reading the text again.
Comparing structured data
JSON, XML and CSV files change constantly in ways that are not semantic: a key moves, a number gains a decimal place, an array is reordered. A textual diff cannot tell a reordered key from a deleted one. Parse both documents, compare keys and values as data, and sort collections before comparing when order carries no meaning. For CSV, be careful with quoting, embedded commas and line endings, since a field containing a comma can look like an extra column to a naive diff. Tools that understand the structure produce a much shorter and more trustworthy report than a raw text comparison.
Checklist for a trustworthy comparison
- Confirm both inputs use the same encoding and line endings.
- Use line mode for a first pass, then drill into changed lines with word or character mode.
- For structured files, compare parsed data instead of the raw text.
Our Text Diff Tool lets you switch between whole-line and character-level comparison and highlights invisible whitespace. Related guides: JSON vs YAML for configuration and counting words and reading time.