"What changed?" is one of the most common questions in software development. Whether you're reviewing a pull request, comparing config files between environments, or checking whether an API response has shifted, a text diff gives you the answer instantly.

This article explains how diffs work, what the output means, and where to use them beyond just code review.

What is a Text Diff?

A text diff compares two versions of a text file and highlights what's been added, removed, or changed. The word "diff" comes from the Unix diff command, which has been around since 1974 and still powers most of what you see in Git, GitHub, and code editors today.

The green and red lines in a GitHub pull request? That's a diff. The "Changes" panel in VS Code? Also a diff.

How Diffs Work

Line-level comparison

The most common form compares text line by line. Lines that were removed are shown with a - prefix (usually in red), and lines that were added get a + prefix (usually in green):

- const greeting = "Hello, World!";
+ const greeting = "Hello, everyone!";

Character-level comparison

Some tools go further and highlight exactly which characters within a line changed. This is especially useful when only a single word was changed in a long line — otherwise the entire line appears modified.

Side-by-side view

Instead of interleaving old and new lines, a side-by-side diff places the original on the left and the revised version on the right. This gives more context and is the preferred view for code review tools.

Practical uses beyond code review

  • Code review: see exactly what a PR adds, removes, and modifies
  • Config file comparison: compare .env files or nginx.conf between environments
  • API response tracking: verify that a JSON response hasn't changed unexpectedly
  • Document editing: track changes in a spec, README, or blog post draft
  • Database migrations: confirm what a migration script actually changes

Git diff commands

# Changes not yet staged
git diff

# Staged changes ready to commit
git diff --staged

# Compare two branches
git diff main feature/new-button

# Changes to a specific file since last commit
git diff HEAD~1 -- src/app.js

Frequently Asked Questions

Does whitespace count as a change?
By default, yes — a tab converted to two spaces shows up as a changed line. Most diff tools have a "ignore whitespace" option that strips this noise so you can focus on actual logic changes. In Git, git diff -w ignores all whitespace differences.
Why does reordering lines cause so many diff changes?
Standard line-based diff algorithms (like Myers diff) don't understand intent — they just compare sequences of lines. Moving a block of code to a different location shows up as a delete at the old position and an add at the new one. For JSON or structured data, a format-aware diff tool that understands the structure will give more meaningful results.
Can I diff binary files like PDFs or images?
Standard diff tools can detect that a binary file changed but can't show what changed inside it. For PDFs, extracting the text layer first and then diffing the text is a workaround. For images, visual diff tools exist that highlight pixel-level differences, though they're a separate category from text diffs.

Summary

  • A text diff shows exactly what was added, removed, or changed between two text versions
  • - lines are removals, + lines are additions
  • Useful for code review, config comparison, API response tracking, and document editing
  • Character-level and side-by-side modes give more context for complex changes

Compare any two pieces of text instantly in your browser:

  • Text Diff Checker — paste two texts and see line-by-line changes highlighted
  • JSON Diff — compare JSON objects with structure-aware diffing
  • Word Counter — check character and line counts for both versions