How a line diff works

A diff splits both texts into lines and looks for the longest common subsequence: the largest set of lines that appear in both, in the same order, though not necessarily next to each other. Every line outside that set was either removed from the original or added in the changed text. Finding the fewest removals and additions is called the shortest edit script problem.

This page uses Eugene Myers' O(ND) algorithm from 1986, the same family of algorithm behind git diff and GNU diff. Its cost grows with the length of the input (N) times the number of differences (D), so two long files with a handful of edits compare almost instantly. Lines inside a changed block are then paired up and compared again word by word, which is where the stronger highlights come from.

Windows line endings (CRLF) count the same as Unix ones (LF): the browser converts them when you paste, so a file saved on Windows never differs on every line.

Reading unified diff output

A unified diff starts with two file headers, --- original and +++ changed, followed by hunks. Each hunk opens with a header such as @@ -12,7 +12,8 @@:

A count of 1 is left out (@@ -3 +3 @@), and a count of 0 means that side has no lines there, as when a block is added to an empty file. \ No newline at end of file marks a last line without a line break. Copy patch gives you this format with 3 lines of context, ready for a code review comment, or for patch once you put real file names in the headers.

Split or unified

Split view puts the original on the left and the changed text on the right, aligned row by row. It reads best when a block was rewritten, because you see the before and after next to each other. Unified view interleaves removed and added lines in one column. It is denser, fits a phone screen, and matches what you see in a terminal or a patch file.

The ignore-whitespace caveat

With Ignore whitespace on, runs of spaces and tabs count as one space and whitespace at the start and end of a line is dropped. That hides re-indentation and trailing spaces so the real edits stand out. But whitespace is meaningful in YAML, Python, Makefiles and Markdown, so "No differences" with this option on does not prove two files behave the same. Turn it off before you trust a result, and don't apply a patch copied with it on.

When to use git diff or diff -u instead

This page is for quick comparisons: two config snippets, a log before and after, a paragraph you rewrote. For files on disk, the command line does more:

They also handle whole directories, renames, binary files and patches you can apply directly.

FAQ

How do I compare two text files online without uploading them?

Open each file, copy its contents, and paste them into the Original and Changed boxes on this page. The comparison runs in JavaScript inside the page: nothing is sent to a server or saved, and closing the tab discards both texts.

What does @@ -1,3 +1,4 @@ mean?

It is a hunk header from a unified diff. -1,3 means the hunk covers 3 lines of the original file starting at line 1, and +1,4 means it covers 4 lines of the changed file starting at line 1. The lines below it start with a space (unchanged), a minus (removed) or a plus (added).

Can I ignore whitespace changes?

Yes. Tick Ignore whitespace and lines are compared with runs of spaces and tabs treated as one space and leading and trailing whitespace dropped, so re-indented or re-spaced lines count as unchanged. Spaces that split a word still count: foo bar and foobar are different. Leave it off for YAML, Python or Makefiles, where whitespace changes meaning.

Why is a line marked as changed when it looks the same?

Usually an invisible character: a trailing space, a tab instead of spaces, a non-breaking space, or a missing newline at the end of the file. The stronger highlight inside the line shows where the difference is. Windows (CRLF) and Unix (LF) line endings are normalised before comparing, so they never cause this on their own.

How large a text can it compare?

Tens of thousands of lines. Texts that are mostly the same compare almost instantly at any size. Above 20,000 lines the page shows a notice, and if two very large texts have little in common it stops refining after a few seconds and shows some changes as whole replaced blocks.