← All guides

How to find the difference between two texts: how diff works and tips

· 2 min read

You may want to see what changed between two versions of a contract, two configuration files or a text you copied and its original. Tools that do this are called "diff". The idea is simple: find what the two texts share and count the rest as added or removed.

Comparing line by line

Most diff tools split the text into lines and compare the lines. A line counts as "changed" even if only one letter differs. That is why highlighting the changed words inside a changed line makes the job easier.

The longest common sequence

The core idea is to find the longest sequence of lines that appears in both texts in the same order. Lines outside that sequence are marked "removed" if they are in the old text and "added" if they are in the new one.

old:  apple / pear / cherry
new:  apple / banana / cherry

common sequence: apple, cherry
result:          - pear
                 + banana

Whitespace and case differences

Two lines that look identical can differ because of a trailing space or a different indent. To cut this noise, ignore whitespace differences and, if needed, ignore letter case.

See only the differences in long texts

In a text with hundreds of lines you do not need to read everything to find a few changes. A "show only differences" option shows a few lines around each change and hides the rest.

Try it yourself

Paste the old and the new text into the diff checker: added lines appear in green, removed lines in red and changed words are highlighted too. Your texts never leave your browser.

This article is general information. Check your organisation's rules before pasting confidential or sensitive text into any service.

More guides