How to Compare Text Files — A Guide to Diff Checking
Comparing two versions of a file is one of the most common tasks in software development — and beyond. Whether you’re reviewing code, auditing config changes, or checking contract revisions, understanding diff output saves time and prevents mistakes.
What Is a Diff?
A “diff” is the computed difference between two pieces of text. The concept comes from the Unix diff command, first released in 1974. It compares two inputs and outputs the minimal set of changes needed to transform one into the other.
Line Diff vs. Character Diff
Line-level diff marks entire lines as added, removed, or unchanged. This is what git diff shows by default and works best for structured text like source code where changes tend to affect whole lines.
Character-level diff (or word-level) highlights the exact characters that changed within a line. This is more useful for prose, configuration values, or any case where a small edit within a long line would be hard to spot in a line diff.
Most modern diff tools combine both — showing line-level changes with intra-line highlighting.
Reading Unified Diff Output
The most common diff format you’ll encounter is “unified diff,” used by Git and most tools:
@@ -1,4 +1,4 @@
server:
host: localhost
- port: 3000
+ port: 8080
debug: true@@lines show the location in each file (line numbers and count)- Lines starting with
-were removed from the original - Lines starting with
+were added in the new version - Lines with no prefix are unchanged context
Common Use Cases
- Code review — every pull request is fundamentally a diff. Reviewing changes line by line catches bugs before they ship.
- Configuration auditing — compare production vs. staging configs to spot unintended differences.
- Contract and document revisions — character diff is invaluable for catching subtle wording changes in legal or business documents.
- Database migration verification — diff the schema before and after a migration to confirm only intended changes were applied.
- Debugging — compare working vs. broken output to narrow down what changed.
Tips for Effective Diffing
- Normalize whitespace — trailing spaces and mixed line endings (CRLF vs. LF) create noise. Strip them before comparing.
- Ignore irrelevant changes — many tools let you ignore whitespace, case, or blank lines to focus on meaningful differences.
- Use side-by-side view for long files where scrolling through unified diff gets disorienting.
- Save diffs as patches — a diff can be applied later with
patchorgit apply, making changes portable.
Command-Line Basics
# Basic file comparison
diff old.txt new.txt
# Unified format with 3 lines of context
diff -u old.txt new.txt
# Git diff of staged changes
git diff --cachedFor quick, visual comparisons without installing anything, an online diff checker works well — paste two blocks of text and instantly see what changed.
Related Tools
- Diff Checker — compare two texts side by side with line and character diffs
- JSON Formatter — format JSON before comparing to eliminate whitespace noise
Try it yourself
Use our free Diff Checker — no signup, no ads interrupting your workflow.
Open Diff Checker