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:
@@ -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?▼
What is a "split diff" vs "unified diff"?▼
Can I compare two text files without Git?▼
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.
Related Articles
YAML vs JSON vs XML
A comprehensive comparison of YAML, JSON, and XML. Learn their differences, performance considerations, and best use cases for configuration and APIs.
Base64 Encoding Explained
Learn how Base64 encoding works, why we use it for images and email, and the difference between encoding and encryption.