How a diff checker finds what changed
A diff doesn't hunt for changes directly. It looks for the longest run of words (or lines) that both versions share in the same order, known as the longest common subsequence. Anything in the original that isn't part of that shared run was deleted, and anything in the new version that isn't part of it was added.
Example: One sentence, two edits
- Original: The quick brown fox jumps over the lazy dog.
- Changed: The quick red fox jumped over the lazy dog.
- Shared, in order: The quick, fox, over the lazy dog.
- Not shared: brown and jumps (removed), red and jumped (added)
Two words swapped, and everything else confirmed unchanged.
The approach goes back to the Unix diff program. Hunt and McIlroy's 1976 Bell Labs report describes diff as solving the longest common subsequence problem to find the lines that don't change between two files. Before comparing, this tool also sets aside any identical opening and closing sections, since most edits touch only a small part of a document.
What one change looks like by words and by lines
| Change | Compared by words | Compared by lines |
|---|---|---|
| One word edited in a long line | Just that word | The whole line, removed and added |
| A comma added after a word | That word, because punctuation sticks to it | The whole line |
| “10am” changed to “10 am” | 10am removed, 10 am added | The whole line |
| A paragraph moved elsewhere | Removed in one place, added in the other | The same |
Because punctuation counts as part of the word it touches, “delivery.” and “delivery,” are different words. That's useful for proofreading, since every changed comma shows, but a heavily repunctuated paragraph lights up more than its wording changes suggest.
Comparing code, data and lists line by line
For code, settings files, CSV exports and lists, compare by lines. Each line usually stands for one thing, whether a statement, a setting or a record, so a changed line is the natural unit to report. It also keeps the result readable: one changed value shows as one line out and one line in, not a scatter of fragments.
Example: Two versions of a shopping list
- Original lines: milk, eggs, bread, butter
- Changed lines: milk, bread, butter, jam
- Result: eggs removed, jam added, the other three unchanged
Lines mode finds the two real changes even though everything after eggs moved up a line.
If the order of a list doesn't matter at all, only which items are on it, Compare Two Lists is the better fit. It ignores order and repeats, and sorts every item into only in the first list, only in the second, or in both.
Checking a contract or policy for quiet changes
Example: A revised payment clause
- Original: Payment is due within 30 days of delivery.
- Revised: Payment is due within 15 days of the invoice date.
- Removed: 30 and delivery.
- Added: 15 and the invoice date.
Two small edits halve the time to pay and move the day the clock starts. Both are easy to miss when you only read the new version.
In a contract, lease or policy, give the most attention to numbers, dates, names, small negations such as not, no and never, and the difference between may and must. Those are the edits that change what a document means while barely changing how it looks.
The same check works for essays and reports. Compare your draft with the copy a tutor or colleague edited and every suggestion shows at once, including the easy-to-miss ones like a deleted “not” or a changed number.
Paste plain text on both sides. Text copied from two PDFs breaks lines in different places, which makes nearly every line look changed, so run both through Clean PDF Text first.
Why a small edit can look like a big one
- Reflowed paragraphs. If one version has a line break every 70 or so characters and the other has none, lines mode marks everything. Switch to words.
- Moved text. A diff only knows removed and added, so a paragraph moved from the start to the end appears twice, once in each color.
- Curly and straight quotes. One program may turn "quotes" into “quotes”, changing every quoted word. Make them match with Find and Replace before comparing.
- Spacing. In words mode, an extra space or an extra blank line is a change of its own and shows up between the words.
- Very long, very different texts. Past a certain size, the changed middle section is shown as one block removed and one block added so the page stays responsive. Compare a section at a time for detail.