How to Compare Two Text Files: 5 Methods for Finding Differences
Compare two texts or files to spot every difference. Learn online diff tools, command-line diff, VS Code comparison, Git diff, and when to use each method.
Try our free Text Diff Checker
Compare two texts side by side with line-level and character-level diff highlighting.
Why Compare Text Files?
Comparing two pieces of text is one of the most fundamental tasks in software development, writing, and document management. Whether you are reviewing code, auditing contracts, checking configuration changes, or editing content, finding the exact differences between two versions of a file can save hours of manual review and prevent costly mistakes.
Code review is the most common use case. When a teammate submits a pull request, you need to see exactly what changed — which lines were added, removed, or modified. Without a diff tool, reviewing even a 50-line change in a 500-line file would mean reading both versions side by side, line by line.
Contract and legal document versioning is another critical scenario. When a client sends back a revised contract, you need to know every word that changed. A single altered clause — a shifted deadline, a different liability cap — can have significant financial consequences. Diff tools catch changes that human eyes miss.
Configuration file changes are a frequent source of production incidents. When a deployment fails, comparing the current config with the previous version often reveals the culprit — a changed port number, a removed environment variable, or a typo in a database connection string.
Content editing and copywriting also benefits from diff comparison. Editors can track exactly what an author changed between drafts. Writers can see how their editor's suggestions altered the tone or structure of a piece. Tools like our Word Counter help you track length changes, while a diff tool shows you the specific edits.
Method 1 — Online Diff Tools
The fastest way to compare two texts is to use an online diff tool. No installation, no command-line knowledge, no setup — just paste your two texts and see the differences instantly.
Our Text Diff Checker lets you paste the original text on the left and the modified text on the right. It highlights differences at both the line level and the character level, so you can spot even single-character changes like a comma becoming a semicolon.
Online diff tools typically offer two display modes:
- 1. Side-by-side view — The original text appears on the left and the modified text on the right. Changed lines are aligned horizontally so you can see what each line looked like before and after. This is ideal for reviewing large files where context matters.
- 2. Inline (unified) view — Both versions are merged into a single column. Removed lines are highlighted in red and added lines in green. This is more compact and works well for small changes or when you want to copy the diff output.
When to use online diff tools: Quick one-off comparisons, sharing results with non-technical colleagues, comparing text that is not in a file (clipboard content, database records, API responses), or when you are on a machine where you cannot install software.
Privacy note:ToolZip's diff checker processes everything in your browser. Your text is never sent to a server. This makes it safe for comparing sensitive content like credentials, internal documentation, or proprietary code snippets.
Method 2 — Command-Line diff
The diff command is the classic Unix tool for comparing files. It has been available on every Mac and Linux system since the 1970s and is installed by default on virtually every Unix-like operating system. On Windows, you can use it through Git Bash, WSL, or Cygwin.
Basic Comparison
Compare two files with the default output format:
# Basic diff — shows which lines differ diff file1.txt file2.txt # Output example: # 3c3 # < The old line in file1 # --- # > The new line in file2
Unified Format (-u)
The unified format is much easier to read and is the standard for patches:
# Unified diff — the most readable format diff -u file1.txt file2.txt # Output example: # --- file1.txt 2026-05-12 10:00:00 # +++ file2.txt 2026-05-12 10:30:00 # @@ -1,5 +1,5 @@ # Line 1 # Line 2 # -Old line 3 # +New line 3 # Line 4 # Line 5
Useful Flags
# Ignore whitespace differences diff -u -w file1.txt file2.txt # Ignore case differences diff -u -i file1.txt file2.txt # Compare directories recursively diff -r dir1/ dir2/ # Show only whether files differ (no details) diff -q file1.txt file2.txt # Side-by-side output in the terminal diff -y file1.txt file2.txt
The command-line diff is perfect for scripting, CI/CD pipelines, and automation. You can pipe its output to other tools, save it as a patch file with diff -u file1 file2 > changes.patch, and apply it later with patch < changes.patch.
Method 3 — VS Code Built-In Compare
Visual Studio Code has a powerful built-in file comparison feature that most developers do not know about. It provides a rich side-by-side view with syntax highlighting, inline change highlighting, and navigation between differences.
How to Compare Two Files
- 1. From the Explorer: Right-click the first file and select Select for Compare. Then right-click the second file and select Compare with Selected.
- 2. From the Command Palette: Press
Ctrl+Shift+P(orCmd+Shift+Pon Mac) and type Compare Active File With... to compare the current file with any other file in your workspace. - 3. From the terminal: Run
code --diff file1.txt file2.txtto open VS Code directly in diff mode.
# Open VS Code diff from the command line code --diff original.txt modified.txt # Compare a file against its last Git commit # (VS Code does this automatically in the Source Control panel)
VS Code's diff view highlights changes at the character level, not just the line level. If a single word changes in a long line, VS Code highlights just that word in a darker shade, making it easy to spot the exact edit. Use the arrow buttons in the toolbar to jump between changes.
Pro tip: You can also compare clipboard content. Open a new untitled file, paste the first text, open another untitled file, paste the second text, then use the Select for Compare method. This turns VS Code into a local diff tool without needing any extensions.
Method 4 — Git Diff
If your files are in a Git repository, git diff is the most powerful way to compare changes. It understands your project history, so you can compare any two points in time — not just two files.
Common git diff Commands
# Show unstaged changes (working directory vs staging area) git diff # Show staged changes (what will be committed) git diff --staged # Compare with the last commit git diff HEAD # Compare two branches git diff main..feature-branch # Compare a specific file between branches git diff main..feature-branch -- src/config.ts # Compare two commits git diff abc123 def456 # Show only file names that changed git diff --name-only main..feature-branch # Show a summary of changes (insertions/deletions per file) git diff --stat main..feature-branch
Reading git diff Output
Understanding the output format is essential:
diff --git a/src/config.ts b/src/config.ts
index 3a4b5c6..7d8e9f0 100644
--- a/src/config.ts # Original file (a)
+++ b/src/config.ts # Modified file (b)
@@ -10,7 +10,7 @@ # Line numbers: starts at line 10
const config = {
port: 3000,
- database: "localhost", # Removed (red)
+ database: "db.prod.com", # Added (green)
timeout: 5000,
};Lines starting with - were removed from the original. Lines starting with + were added in the new version. Lines without a prefix are unchanged context lines.
Pro tip: Use git diff --word-diff to see changes at the word level instead of line level. This is especially useful for prose and documentation where edits tend to be small wording changes within long lines.
Method 5 — Programmatic Comparison
When you need to compare text as part of an application — for example, building a version history feature, a content management system, or an automated testing pipeline — you can use diff libraries in your programming language of choice.
JavaScript: diff Library
The diff npm package is the most popular JavaScript library for text comparison:
import { diffLines, diffWords } from 'diff';
const oldText = "Hello World\nThis is a test.\nGoodbye.";
const newText = "Hello World\nThis is an example.\nGoodbye.";
// Line-level diff
const lineDiff = diffLines(oldText, newText);
lineDiff.forEach(part => {
const prefix = part.added ? '+' : part.removed ? '-' : ' ';
console.log(prefix, part.value);
});
// Word-level diff (useful for inline highlighting)
const wordDiff = diffWords(
"The quick brown fox",
"The slow brown cat"
);
// Result: "The " (unchanged), "quick" (removed),
// "slow" (added), " brown " (unchanged),
// "fox" (removed), "cat" (added)Python: difflib
Python's standard library includes difflib, so no installation is needed:
import difflib
old_text = """Hello World
This is a test.
Goodbye.""".splitlines()
new_text = """Hello World
This is an example.
Goodbye.""".splitlines()
# Unified diff (like diff -u)
diff = difflib.unified_diff(
old_text, new_text,
fromfile="original.txt",
tofile="modified.txt",
lineterm=""
)
print("\n".join(diff))
# HTML side-by-side diff
differ = difflib.HtmlDiff()
html = differ.make_file(old_text, new_text)
with open("diff_report.html", "w") as f:
f.write(html)The difflib.HtmlDiff class generates a complete HTML page with a side-by-side comparison table, which is useful for generating diff reports that can be shared via email or viewed in a browser.
For rendering diffs in a web application, you can convert your Markdown to HTML for documentation and combine it with diff output for a rich version comparison view.
Frequently Asked Questions
How do I compare two documents for differences?
What is the difference between inline and side-by-side diff?
Can I compare Word or PDF documents online?
How does git diff work?
Compare Your Texts Now
Paste two texts, see every difference highlighted instantly. Line-level and character-level diffs. Free, private, runs in your browser.
Open Text Diff Checker