How to compare two files or code online

See exactly what changed between two versions of a file or piece of code, and how to read the result, including a unified diff.

You have two versions of something: a config file before and after an edit, two drafts of a document, or a function before and after a refactor. You want to see what is different without reading both top to bottom. That is what a diff does, and reading one well takes only a few ideas.

What a diff shows

A diff is the shortest list of lines to remove and add to turn the first text into the second. Lines that are the same on both sides are left alone, so what you see is only what changed. A good comparison also marks which words changed inside a changed line, so a one-character edit does not look like a whole new line.

Compare two files step by step

  1. Put the older version on the left and the newer one on the right. Swapped, every addition shows as a removal.
  2. Paste the text or drop the files in. In the diff checker the files are read on your machine and never uploaded.
  3. Read red and green. Red lines were removed, green lines were added, and changed words inside a line are marked.
  4. Move through the changes with the arrows instead of scrolling a long file.
Open the Diff checker

When everything looks changed

A comparison that lights up the whole file usually has one of three causes:

  • Line endings. A file saved on Windows ends each line with CRLF, one saved on Linux or macOS with LF. Turn on ignore whitespace to compare only the text.
  • Indentation. Tabs versus spaces, or a different indent width, changes the start of every line.
  • Case. If capitalisation does not matter for your comparison, ignore case so it does not hide the real edits.

For code, run both versions through the code beautifier first so formatting differences disappear and only real changes remain. For JSON, use the JSON diff, which also sorts keys.

Reading a unified diff

Git and the diff -u command write changes in unified format. A small example, a config file where two lines changed and one was added:

--- a/config.old.yml
+++ b/config.yml
@@ -1,4 +1,5 @@
 host: localhost
-port: 8080
-debug: true
+port: 9090
+debug: false
 retries: 3
+timeout: 30
  • Lines starting with - were removed and lines starting with + were added. Lines starting with a space are unchanged context.
  • The @@ -1,4 +1,5 @@ line says where the change sits: the old file’s lines 1 to 4 became the new file’s lines 1 to 5.
  • The first two lines name the old file (---) and the new file (+++).

The diff checker can download exactly this as a .patch file, which tools like git apply and patch can apply to the original.

Common problems

  • Sharing a link with private text. A share link carries both texts inside it, so anyone with the link can read them. Do not share links that contain passwords, keys or private data.
  • Comparing minified or reordered data as plain text. One long line hides the real change. Format the data first, or use a tool made for that format.
  • Expecting a diff to understand meaning. A diff compares characters and lines. Moving a block of code shows as a removal in one place and an addition in another.
Open the Line cleaner

The line cleaner removes duplicate and blank lines and sorts lines, which helps when only the order or the blank lines differ.