+−DiffBundle

How to read diff output without misreading it

9 min readUpdated September 2026100% local

Lines of source code on a dark screen, the kind of output a unified diff is produced from.

A diff is a compressed description of how to turn one document into another, and like any compressed format it is easy to misread. Reviewers routinely approve changes they did not understand, or spend twenty minutes on a hundred-line diff that turns out to be a formatter run. This guide covers the anatomy of a unified diff, when a side-by-side view beats it, the handful of invisible characters responsible for most false alarms, and the cases where comparing text is simply the wrong operation.

The anatomy of a unified diff

A unified diff opens with two file headers: three minus signs and the original path, three plus signs and the new path. Everything after that is a sequence of hunks, and each hunk starts with a line like @@ -84,7 +84,9 @@. The numbers are read as “from the old file, seven lines starting at line 84; in the new file, nine lines starting at line 84” — so this hunk adds two lines net.

Inside a hunk the first character of every line is the instruction. A space means the line is unchanged and is present only as context; a minus means it exists in the old version and not the new; a plus means the opposite. A modified line is not a special case — it appears as a removal immediately followed by an addition, which is why a single retyped word shows up as two lines.

Two details catch people out. The three lines of context above and below each change are configurable, and widening them merges nearby hunks into one; that is why the same change can render as three hunks or one. And the line “\ No newline at end of file” is not an error — it records that a file’s last line lacks a trailing newline, a real difference that some editors add or remove silently.

Unified or side by side?

The unified view is compact, pastes cleanly into a ticket, and is the format patch tools actually consume. It is the better choice for long files with scattered small edits, for review on a narrow screen, and whenever you intend to apply the result rather than only look at it.

A side-by-side view wins when the two versions are structurally similar and you need to compare them as wholes — rewritten paragraphs, reordered configuration blocks, a table where a column shifted. Alignment does the work your eyes would otherwise do, and a block that moved is visible as a gap on one side rather than as a deletion and an unrelated insertion forty lines apart.

Either view benefits from word-level highlighting inside changed lines. Without it, a changed line is an opaque red/green pair and you must re-read both versions character by character; with it, the two characters that actually differ are marked and the rest stays quiet. For long lines — URLs, minified content, a sentence in a document — this is the single biggest readability win available.

Why a one-character change produces a hundred-line diff

Diff algorithms work on whole lines. Anything that rewrites a line — even without changing what it means — makes that line a deletion plus an insertion. Re-indenting a block, wrapping it in a condition, running a formatter with different settings, or converting tabs to spaces can therefore produce a diff in which every line is marked as changed and none of them is.

The fix is to separate the passes. Commit the formatting change on its own, review it as a single “no functional change” unit, and keep the real edit in its own commit where the diff is three lines long. When you receive a mixed diff instead, most tools offer an ignore-whitespace mode that collapses the noise and leaves the substance.

Beware of the opposite failure. Ignoring whitespace hides real bugs in languages where indentation is syntax (Python, YAML, Makefiles) and in any content where spacing is data — fixed-width records, Markdown code blocks, the contents of a string literal. Turn the option on to understand a diff, then turn it off before you approve one.

Line endings and other invisible characters

Windows ends a line with a carriage return and a line feed, Unix with a line feed alone. A file that crosses that boundary differs on every single line while looking identical in every editor. Git’s core.autocrlf and a .gitattributes with text=auto exist precisely to normalize this; without them, one contributor on a different operating system can produce a pull request that touches the whole file.

Encoding adds its own surprises. A UTF-8 byte order mark at the start of a file is three invisible bytes that break shebang lines and JSON parsers while showing nothing on screen. Non-breaking spaces pasted from a document, zero-width joiners copied from a chat client, and Windows-1251 text opened as UTF-8 all render as either nothing or a single odd glyph, yet they are genuine byte differences.

When a diff insists two lines differ and they look the same, stop reading and start measuring. Compare the lengths, look at the bytes, or switch on the tool’s whitespace-visualization. The character you cannot see is nearly always a trailing space, a tab, a CR or a BOM — and identifying which one takes seconds once you stop trusting the rendering.

When comparing text is the wrong operation

For structured formats, a line-based diff answers a question nobody asked. Two JSON documents with the same content but different key order, different indentation, or 1.0 written as 1 are semantically identical and textually unrecognizable. A structural comparison parses both sides, walks the trees and reports paths instead of lines: added at user.profile.avatar, changed at limits.maxUploadMb from 10 to 25.

YAML raises the stakes because the same document can be written in flow or block style, with or without quotes, and with anchors that expand on load. CSV has the same problem in another shape: a row-based text diff of an export that was re-sorted reports every row as changed, while a key-based comparison matches rows by their identifier column and reports the three cells that actually moved.

The rule of thumb: if the format has a parser, compare after parsing. Keep the text diff as the fallback for when a file will not parse — a truncated export, a syntax error, a merge conflict marker left behind — because then the raw bytes are exactly what you need to see.

What a diff cannot tell you

A diff describes an edit, not a reason. It cannot distinguish a deliberate change from a bad merge, a rename from a delete-plus-create, or a genuine rewrite from a block of code that simply moved. Many tools guess at renames by similarity score, and the guess is usually right and occasionally spectacularly wrong.

It also says nothing about equivalence of behaviour. Two versions of a function can differ on every line and do exactly the same thing; two versions can differ by one character and behave completely differently. The size of a diff is a measure of textual distance, never of risk — the riskiest change of the week is frequently a single flipped comparison operator.

So read a diff as a list of questions rather than a verdict. For each hunk ask what it changes in behaviour, whether anything outside the hunk depends on it, and what the author was trying to achieve. The diff points at the lines; understanding what they do is still the reviewer’s job.

Text diffRelated toolOpen this comparison mode