What Is Diff?
Diff (short for difference) refers to both the algorithm for computing changes between two texts and the resulting output. Introduced in Unix systems in the 1970s, it has become core infrastructure for software development — version control (Git), document editors, code review tools, and deployment verification all rely on it.
The goal of a diff algorithm is to find the minimum number of edit operations (insertions and deletions) needed to transform one sequence into another. This minimum set is known as the Minimum Edit Distance, or Levenshtein Distance.
LCS: Longest Common Subsequence
The mathematical foundation of diff is LCS — finding the longest sequence of elements that appears in both inputs in the same order. LCS is solved using dynamic programming with O(mn) time complexity, where m and n are the lengths of the two sequences.
Once the LCS is found, lines not in the LCS are the changed ones: lines shared in the LCS are unchanged (context), lines only in the original are deletions (-), and lines only in the modified version are insertions (+).
Myers Diff Algorithm
Git uses the Myers diff algorithm, published by Eugene Myers in 1986. It's LCS-based but runs in O((N+M)D) time — where D is the edit distance — making it dramatically faster than pure LCS when changes are small (which is typical in code edits).
Myers frames the problem as finding the shortest path through an edit graph. Moving right represents an insertion, moving down represents a deletion, and diagonal moves represent unchanged lines (matching elements).
Reading Git Diff Output
diff --git a/src/utils.js b/src/utils.js
index 4b825dc..9f3c2e1 100644
--- a/src/utils.js # original file
+++ b/src/utils.js # modified file
@@ -10,7 +10,8 @@ # hunk header
function formatDate(date) {
- return date.toLocaleDateString(); # deleted line
+ const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
+ return date.toLocaleDateString('en-US', options); # added line
}
The hunk header @@ -10,7 +10,8 @@ means: show 7 lines from line 10 in the original, and 8 lines from line 10 in the modified version. Lines prefixed with a space are unchanged context; - means deleted; + means added.
Practical Application: Code Review
For large refactors where whitespace changes add noise, use git diff -w (ignore whitespace) to focus on logical changes. For renames combined with content changes, git diff -M enables rename detection so the diff shows a modification rather than a delete+add pair.
To view the diff for a specific commit range: git diff commit1..commit2. To restrict to a single file: git diff HEAD~1 -- src/utils.js.
Practical Application: Deployment Verification
Reviewing config file changes before deployment is one of the most effective ways to prevent production incidents. Build a team practice of diffing environment variables (.env), Nginx configs, and database migration scripts before every deploy. In CI/CD pipelines, adding an automated step that diffs key build artifacts against the previous release can catch unexpected changes early.
Line-Based vs Word-Based Diff
Line-based diff is the traditional approach — fast but marks the whole line as changed even if only one word differs. Word-based diff provides finer granularity and is more useful for prose documents or lines with long content.
In Git, use git diff --word-diff for word-level comparison. On GitHub, the "Split" view shows side-by-side line diff; "Unified" shows the classic unified format.
FAQ
Why does a single whitespace difference produce such a large diff?
In line-based diff, even a single space difference marks the entire line as changed. The best fix is to use a shared formatter (Prettier, ESLint) across your team and apply automatic formatting before committing. You can also use git diff -w to view the diff ignoring whitespace.
Can binary files be compared with diff?
Standard text diff doesn't apply to binary files. Git shows "Binary files differ" for them. For images, PDFs, and other binary files, hash comparison (SHA-256) is the efficient way to check if two files are identical.
When should I use patience or histogram diff instead of Myers?
Myers is the default and works well for most code changes. The patience algorithm produces more readable diffs when files have many repeated lines (like XML or JSON). Histogram is an improvement on patience and tends to give better results for similar content. Use --diff-algorithm=histogram when Myers produces confusing diffs.
Try the Tool
Compare two text snippets directly in your browser using the Text Diff tool. It displays additions and deletions in a GitHub-style side-by-side view with synchronized scrolling.