HSHimanshu Sharma
Free Tools

How a text diff works: compare two versions and see exactly what changed

Advertisement

Two versions of the same text sit in front of you. An editor changed your article, a colleague updated a contract, a config file works on one server and not on another. Reading both side by side, line by line, is slow and your eyes will miss the one word that matters. A diff (short for difference) does that comparison for you and highlights exactly what was added and what was removed. The free Text Diff Checker does it in the browser: paste the original on the left, the changed version on the right, and read the result. This guide explains how the comparison works, which mode to choose and the traps that create false differences.

What the tool shows

Take two small versions:

Original:                         Changed:
The quick brown fox               The quick red fox
jumps over the lazy dog           jumps over the sleepy dog
                                  and runs away

Compared line by line, the first two lines of the original are reported as removed and the three lines of the changed version as added, because no line is exactly the same. In the output removed text is shown in red and added text in green, and a counter tells you how many pieces were added and how many removed. Compared word by word, the picture is far more precise: only brown is removed and red added, lazy becomes sleepy, and and runs away is added at the end. The rest is shown as unchanged.

How a diff finds the changes

The tool uses the classic method of finding the longest common subsequence. A subsequence is a set of items taken in their original order, not necessarily next to each other. The longest common subsequence of two texts is the longest list of lines (or words) that appears, in order, in both. Everything in the original outside that list must have been removed, and everything in the changed version outside it must have been added.

Imagine comparing the word lists cat sat on mat and cat sat on the mat. The longest common run is cat sat on mat, so the only difference is that the was added. A diff does not know why you changed the text; it only finds the smallest set of additions and removals that turns one version into the other. The same idea powers Git, code review tools and the "track changes" view in word processors.

There can be more than one valid answer. If a line was moved from the top to the bottom, a diff reports it as removed in one place and added in another, since it has no concept of "moved". And if the text repeats a lot, two equally short answers may exist, and the tool picks one of them. Both are correct; read the result as a guide, not a story.

Line mode or word mode

Line mode treats each line as one unit. It suits anything where lines are meaningful:

  • source code and configuration files;
  • lists of names, emails or URLs;
  • CSV rows and log files;
  • poems, lyrics and dialogue.

In line mode a single changed character marks the whole line as different. That is exactly what you want in a code review, where a line is a statement, but it is too coarse for prose.

Word mode splits on whitespace and compares the words. It suits paragraphs, articles, emails and contracts, where lines wrap at random places and a small edit should not light up a whole paragraph. If one version wraps its lines in different places, line mode reports almost every line as changed. Word mode still marks the changed line breaks themselves, because it keeps spaces and line breaks as pieces of text, but the words around them stay unchanged, so the real edits are easy to find.

A good routine is to start in line mode for structured text and word mode for flowing text, and to switch when the result looks too noisy or too vague.

Using the Text Diff Checker

  1. Open the Text Diff Checker.
  2. Paste the original text in the left box and the changed text in the right one.
  3. Pick Compare by line or Compare by word.
  4. Read the result: unchanged text is plain, removed text is red and added text is green, with a summary of how many pieces changed.

Each side is limited to about 2,000 lines or words so that the page stays fast. For a longer document, compare it chapter by chapter. Everything is processed locally, so confidential contracts and private notes are not uploaded.

Good uses

  • Reviewing an edit. Paste your draft and the editor's version to see every change before you accept it.
  • Checking a contract or policy update. Compare the old terms with the new ones and read only the changed clauses. This is a common way to spot a quietly changed number or deadline.
  • Finding the difference between two config files. When one server behaves differently, the setting that differs is usually a single line.
  • Comparing two versions of a function. A quick check before you decide which one to keep.
  • Confirming a translation or copy is complete. If a sentence is missing from one version, it stands out.
  • Verifying a copy-paste. Paste the source and your copy to prove nothing was lost.
  • Comparing lists. Two exports of names or product codes show who was added or dropped.

Why identical-looking lines show as different

This is the number one question. Two lines can look the same on screen and still differ, because a diff compares characters exactly. The usual causes are:

Trailing spaces. A space at the end of a line is invisible but counts as a difference. Editors and web forms often add or trim them.

Tabs versus spaces. Indentation made with a tab character differs from indentation made with spaces, even though both look like a gap.

Line endings. Windows ends lines with two characters (carriage return and line feed), while Linux and macOS use one. Text that travelled between systems can differ at every line end, though the tool splits on the newline, so a stray carriage return may remain at the end of each line.

Curly versus straight quotes. Word processors replace " with “ ” and ' with ’. They look alike and are different characters.

Non-breaking spaces and zero-width characters. These come from web pages and chat tools and are completely invisible. If a difference cannot be explained, run both texts through the Invisible Character Cleaner and compare again.

Different capitalisation. The comparison is case-sensitive. Hello and hello are different.

If you only care about the words, normalise the spacing first: paste each version into a text editor, remove the extra spaces and line breaks, and compare again in word mode.

Reading a diff well

  • Scan the colours first, then read each coloured part in context.
  • Expect pairs. A change is usually shown as a red part followed by a green part. Read them together: "brown" became "red".
  • Check the count. If a one-word edit shows hundreds of changes, the two texts probably differ in invisible formatting or line breaks, not content.
  • Do not trust a diff to judge meaning. It cannot tell a typo fix from a changed fact. A small green not can reverse a sentence, so read every highlighted word.

When a different tool is better

This tool is for quick, one-time comparisons. For long documents, whole projects, or a history of changes over time, use version control such as Git. It stores every version, lets you compare any two and merges the work of several people. Word processors also have a built-in compare feature for documents with formatting, images and tables, which a plain-text comparison cannot see.

If you write code, learn the basics of Git; it is built on exactly the algorithm described above. In PHP you can compare strings and arrays in code with functions such as array_diff, which the array functions lesson covers, and in SQL you find rows present in one table but not another with joins, described in the left and right join lesson.

Related tools

Count how many words changed between versions with the Word Counter. Clean copied text of hidden characters first with the Invisible Character Cleaner. If both texts are JSON, format them the same way with the JSON Formatter before comparing, so that differences in spacing do not drown out the real changes. And if you need a quick fingerprint to check whether two files are identical, the Hash & Encode Toolkit produces one.

Quick answers

Is the comparison case-sensitive? Yes. A capital letter and its lower-case form are different characters.

Why does one changed word mark a whole line? In line mode the line is the smallest unit. Switch to word mode for finer detail.

Does it detect moved text? Not as a move. It shows the text as removed from one place and added in another.

Is there a limit? Each side can hold about 2,000 lines or words.

Is my text uploaded? No. The comparison runs entirely in your browser.

Advertisement