Text Diff & Comparison — Complete Guide
What Is a Text Diff?
A text diff is a way of showing what changed between two versions of a file, string, or document. The name comes from the Unix diff utility, which has been around since 1974 — making it one of the oldest and most useful developer tools still in daily use.
Instead of making you read two files side by side and mentally compare them, a diff highlights the exact lines that were added, removed, or modified. This sounds simple, but it powers some of the most important workflows in software development: code review, version control, configuration auditing, and even plagiarism detection.
The Unified Diff Format
When diff tools display results as text, they almost always use the unified diff format. It's the format Git uses for its patch output, and it's what you'll see in most terminal tools. Each change is introduced by a header showing which lines of the old file and new file are affected, then the lines themselves:
--- config.old.yaml
+++ config.new.yaml
@@ -10,4 +10,5 @@
timeout: 30
- retries: 3
+ retries: 5
+ backoff: exponential
debug: false
log_level: info
Lines starting with - were removed, lines with + were added, and lines with a space are unchanged context that helps you see where the change happened. The @@ header tells you the line numbers in each version — here, the change starts at line 10 in the old file and line 10 in the new file, spanning 4 and 5 lines respectively.
Line vs Character Diff
Most diff tools compare line by line: the unit of comparison is a full line of text. That's the right granularity for source code, config files, and prose where each line carries meaning. But line-level diffing has a blind spot — it treats a line that changed by one character as a completely different line.
That's where character-level (inline) diffing comes in. When a line is modified, a good tool drills into it and highlights exactly which characters changed, not just the whole line. This is essential for things like:
- Fixing a typo in a word and wanting to see which letters changed
- Comparing JSON or XML where whitespace differences matter
- Reviewing small refactors where variable names shift by a few characters
Modern diff tools combine both: line-level comparison for structure, character-level highlighting inside changed lines. That combination gives you the "big picture plus fine detail" view.
Common Use Cases
Diffing shows up in more places than you might expect:
- Code review: Pull requests are essentially a diff. Reviewers see only what changed, making it practical to review thousands of lines of code.
- Config comparison: Before deploying to production, teams diff the current config against the proposed one to catch accidental changes like a wrong port or an API key rotation.
- Plagiarism and copy checks: Comparing documents for near-duplicate text — identical blocks show up as zero-diff regions, while lightly edited text produces small, scattered diffs.
- Data migration validation: After transforming data, diff the source and target outputs to confirm nothing was lost or altered.
How Diff Algorithms Work: The Myers Algorithm
Behind the scenes, most diff tools implement an algorithm by Eugene Myers (1986). The Myers algorithm finds the shortest edit script — the minimal set of insertions and deletions that turns one text into another — by searching diagonals on an edit graph. It's what Git uses, and its elegance is that it produces diffs that look "natural" to humans: changes are grouped together instead of scattered randomly.
You don't need to implement Myers yourself — but knowing it exists explains why diffs behave the way they do. For example, when two lines are identical, the algorithm keeps them (the common "anchor" lines), and changes tend to cluster around regions of difference rather than spreading across the whole file.
Try It Live
You don't need a terminal or Git to experiment with diffing. Our Text Diff Tool compares any two texts side by side, highlights added and removed lines in color, and works entirely in your browser — nothing is uploaded to a server.
FAQ
What does "unified diff" mean?
It's a format that shows changed lines together with a small amount of unchanged context around them, using - and + prefixes. Git, patch, and most diff viewers use it.
Is a text diff the same as a file comparison?
Essentially yes. A text diff compares the contents of two texts or files and reports the differences. Binary files (images, executables) need specialized binary diffing instead.
Can a diff show me whitespace changes?
Yes. Most tools can ignore or highlight whitespace — trailing spaces, tabs vs spaces, and line endings (CRLF vs LF) are common sources of phantom diffs worth checking.