Tutorial6 min read

How to Compare Two Text Files or Code Blocks for Differences

Spotting the difference between two versions of a document, config file, or code snippet by eye is error-prone. Learn how diff tools work and how to use them to find changes instantly.

Spotting the difference between two versions of a text file, configuration, or code block by eye is tedious and error-prone. You'll miss things. Diff tools solve this exactly — they show you precisely what changed, added, or removed, line by line.

What Is a Diff?

A "diff" is the output of comparing two text inputs and highlighting the differences between them. The term comes from the Unix diff command, first released in 1974.

A diff shows:

  • Lines that exist only in the first input (removed)
  • Lines that exist only in the second input (added)
  • Lines that are identical in both (unchanged, often hidden to reduce noise)

Diffs are used everywhere in software development: code review, version control (git diff), configuration management, documentation updates, and merge conflict resolution.

When to Use a Text Diff Tool

Code review. When a teammate shares a modified file and you need to understand exactly what changed without reading the entire file.

Configuration comparison. Compare two versions of a config file — YAML, JSON, .env, nginx.conf — to find the exact lines that differ.

Document revision. Check what changed between two drafts of a document, contract, or specification.

Log analysis. Compare two log files to find new errors or missing entries.

Database schema changes. Compare SQL schema dumps before and after a migration.

Testing. Compare expected output vs actual output when diagnosing a failed test.

How to Compare Text Files Online

Use DevZone's Diff Checker to compare any two blocks of text:

  1. Paste the original text in the left panel.
  2. Paste the modified text in the right panel.
  3. The tool highlights additions in green and deletions in red.
  4. Use the "side by side" or "inline" view depending on your preference.

The tool handles everything from two-line snippets to multi-page documents. No files are uploaded — the comparison runs entirely in your browser.

Reading Diff Output

The traditional unified diff format (used by git) uses + and - symbols:

@@ -10,7 +10,8 @@
 function calculateTotal(items) {
-  return items.reduce((sum, item) => sum + item.price, 0);
+  const subtotal = items.reduce((sum, item) => sum + item.price, 0);
+  return applyDiscount(subtotal);
 }
  • Lines starting with - were in the original (removed)
  • Lines starting with + are in the modified version (added)
  • Lines starting with a space are unchanged (context)
  • @@ -10,7 +10,8 @@ is the hunk header: original started at line 10, new starts at line 10 with 8 lines instead of 7

Modern diff tools show color-coded highlights instead of symbols, which is easier to scan.

Comparing Files in the Terminal

Linux/macOS:

diff file1.txt file2.txt

Unified format (what git uses):

diff -u file1.txt file2.txt

Side-by-side view:

diff -y file1.txt file2.txt

Ignore whitespace changes:

diff -w file1.txt file2.txt

Recursive directory comparison:

diff -r dir1/ dir2/

Using git diff

If your files are in a git repository, git diff is the most powerful option:

# Unstaged changes
git diff

# Staged changes
git diff --staged

# Compare two commits
git diff abc123 def456

# Compare two branches
git diff main feature-branch

# Specific file
git diff HEAD~1 -- path/to/file.txt

Word-Level vs Line-Level Diffs

Line-level diffs mark an entire line as changed if anything on that line changed. This can be hard to read for prose changes:

- The quick brown fox jumped over the lazy dog.
+ The quick brown fox leaped over the sleeping dog.

Word-level diffs highlight just the changed words within a line — much easier to read for documents:

The quick brown fox jumped leaped over the lazy sleeping dog.

Most modern diff tools offer both modes. For code, line-level is usually more useful. For prose or configuration values, word-level shows changes more clearly.

Handling Whitespace in Diffs

Whitespace changes — extra spaces, tabs vs spaces, trailing spaces — can clutter a diff with noise when the actual logic hasn't changed. Options for ignoring whitespace:

Tool Option
Unix diff -w (all whitespace), -b (trailing whitespace)
git diff --ignore-space-change, --ignore-all-space
Online diff tools Usually a checkbox or toggle

When reviewing code, it's common to see a reformatting commit first (whitespace-only changes) followed by a logic commit, so reviewers can focus on the meaningful changes.

Three-Way Merge and Conflict Resolution

When two branches modify the same file, version control systems perform a three-way merge using the common ancestor as the base:

Base (ancestor) + Side A changes + Side B changes = Merged result

When both sides changed the same lines, there's a conflict:

<<<<<<< HEAD
function login(user, pass) {
=======
function authenticate(credentials) {
>>>>>>> feature/auth-refactor

The <<<<<<< HEAD to ======= block is your version; ======= to >>>>>>> branch is the incoming version. You choose one, combine them, or write something new.

FAQ

What's the difference between a diff and a patch?

A patch is a diff file applied to transform one file into another. You create a patch with diff -u original.txt modified.txt > changes.patch and apply it with patch original.txt < changes.patch. Patches are how open-source contributions were historically shared before pull requests.

How do I compare two JSON files ignoring key order?

Standard diff tools compare line by line, so JSON with the same keys in different order will show as completely changed. For semantic JSON comparison (treating {a:1, b:2} as equal to {b:2, a:1}), use a JSON-aware diff tool or normalize both files with a formatter first.

Can I diff binary files?

Standard text diff tools only work with text. For binary files (PDFs, images, executables), you need specialized tools. For Word documents, convert to plain text first or use Word's built-in "Compare Documents" feature.

How does git handle merge conflicts?

Git attempts to auto-merge changes. When both branches modify the same lines, git marks the conflict and leaves the file in a conflicted state. You resolve the conflict by editing the file to contain the correct final version, then staging it with git add and completing the merge.

Try the tools