Guides

How to Read a Diff: The Developer's Guide to Comparing Code

6 min read
January 23, 2026
read git diff, diff checker online, compare two text files

Whether you are resolving a merge conflict or reviewing a pull request, reading a diff is a daily task for developers. But staring at a wall of red and green text can be confusing.

This guide explains how diff algorithms identify changes and how to read the standard "Unified Diff" format used by Git and Linux.

What is a Diff?#

A "diff" (difference) tool creates a step-by-step recipe to transform File A into File B. It identifies:

  • Additions: Lines that exist in File B but not File A.
  • Deletions: Lines that exist in File A but not File B.
  • Modifications: Often represented as a deletion followed by an addition.

Understanding Unified Format#

The raw output of `git diff` usually looks like this:

git-diff-example.difftext
@@ -1,4 +1,4 @@
 function calculateTotal(price, tax) {
-  return price + tax;
+  return price * (1 + tax);
 }

Decoding the Symbols#

The Header (@@ -1,4 +1,4 @@)
This confusing metadata tells you exactly where the changes occurred. -1,4 means "Starting at line 1 of the original file, show 4 lines". +1,4 means the same for the new file.

The Lines:

  • Space ( ): Context line. Unchanged. Helps you see where you are.
  • Minus (-): Deleted line. This code was removed.
  • Plus (+): Added line. This code was added.

How It Works (Myers Algorithm)#

Most diff tools (including Git) use the Myers Diff Algorithm. It finds the "shortest edit script" (SES) to turn one text into another.

It views the problem as a graph search, trying to reach the end of the file by making the fewest possible "moves" (insertions or deletions). This is why sometimes diffs look weird—the algorithm found a mathematically short path, even if it doesn't match how a human would explain the change.

Frequently Asked Questions

Why do diffs sometimes show the wrong changes?
Diff algorithms are mathematical, not semantic. They don't "understand" code. If you reformatted a file (changed indentation), the diff might show the entire file as changed.
What is a "split diff" vs "unified diff"?
Unified diff mixes changes in one column (standard for CLI). Split diff (side-by-side) shows files next to each other, which is often easier for humans to read.
Can I compare two text files without Git?
Yes! You can use our online Diff Checker tool to compare any two blocks of text or code instantly in your browser.

Compare Text Side-by-Side

Paste two snippets of text or code and instantly see the differences. Perfect for code reviews, document drafts, and config files.

Try Diff Checker

Related Articles