What Is a Text Diff?
A text diff is a structured comparison of two versions of text that identifies what changed between them — what was added, what was removed, and what stayed the same. The output highlights the differences so a reader can quickly understand what changed without reading both versions in their entirety.
The concept is fundamental to software development — version control systems like Git are built on diff algorithms — but it is equally useful for comparing document revisions, configuration files, data exports, and any situation where two versions of text need to be reconciled.
The Algorithms Behind Diff
The most widely used diff algorithm is based on the longest common subsequence (LCS) problem. Given two sequences, LCS finds the longest sequence of elements that appears in the same order in both. The elements not part of the LCS are the differences — they either appear only in the first version (deletions) or only in the second (additions).
The classic diff algorithm was described by Hunt and McIlroy in 1976 and still underlies the Unix diff tool and many derivatives. For larger inputs, the Myers diff algorithm (1986) improves performance by finding the shortest edit script — the minimum number of insertions and deletions needed to transform one text into the other.
A key distinction is line-level vs character-level diff. Line-level diff treats each line as a unit and shows which complete lines changed. Character-level (or word-level) diff provides finer granularity, showing exactly which words or characters changed within a line. Most diff tools use line-level comparison for the primary view and character-level highlighting within changed lines to show the precise changes.
Reading a Diff
Standard diff output uses a specific convention. Lines prefixed with - were in the original but removed. Lines prefixed with + were added in the new version. Lines with neither prefix are unchanged context lines — shown to help orient the reader.
The visual diff format used in most modern tools (and familiar from GitHub pull request reviews) color-codes the output: red for deletions, green for additions. Side-by-side diffs show the original on the left and the new version on the right, aligned so corresponding sections appear at the same vertical position.
Unified diff format (the output of diff -u) includes context lines and marks the changed section with @@ line numbers @@ headers, indicating which line numbers the section corresponds to in each file.
Code Review
In software development, diffs are the primary artifact of code review. Pull requests on platforms like GitHub, GitLab, and Bitbucket show the diff of the proposed changes against the base branch. Reviewers read the diff to understand what was changed, identify bugs or style issues, and suggest improvements.
Reading diffs effectively is a skill. The best approach is to understand the intent of the change first — read the pull request description, then look at which files changed, then dive into the line-level changes. Large diffs (hundreds of files) are difficult to review thoroughly; well-scoped changes that do one thing are easier to validate.
Diff tools in IDEs like VS Code and IntelliJ IDEA show inline diffs during file editing, highlighting unsaved changes against the last committed version. This instant feedback loop is valuable during development.
Document Editing and Version Control
Writers use diff tools to track revisions between document versions. A research paper going through multiple editing passes, a legal contract being negotiated, or a policy document being updated — comparing versions with a diff tool reveals exactly what changed without requiring the reader to hold both versions in memory simultaneously.
Word processors like Microsoft Word have built-in track changes functionality that serves a similar purpose, but text diff tools work with any plain text or Markdown document and produce more reliable output when comparing large, complex documents.
For academic writing, comparing a submitted manuscript with a revised version makes it easy to verify that all reviewer comments were addressed. For legal documents, a diff between contract versions provides a clear audit trail of what was agreed upon in each negotiation round.
Configuration File Management
System administrators and DevOps engineers use diff tools constantly to manage configuration files. Comparing a production config against a staging config, verifying that a deployment script changed only the intended settings, or reviewing the difference between a template config and a deployed version — all of these are diff tasks.
Infrastructure-as-code tools like Terraform and Ansible display diffs before applying changes, showing exactly what will change in the infrastructure. Understanding these diffs is essential to catching mistakes before they cause production incidents.
Config diff is also important for security. Comparing the current server configuration against a known-good baseline helps detect unauthorized changes — a simple but effective security monitoring technique.
Handling Large Files
Text diff tools handle large files differently depending on implementation. For very large files (tens of megabytes of text), naive LCS algorithms become slow. Practical diff tools use heuristics to break large inputs into chunks and diff each chunk independently, which provides reasonable performance at the cost of occasionally missing optimal alignments across chunk boundaries.
Structured text formats like JSON or XML benefit from semantic diffing — comparing the logical structure rather than the raw text. A semantic JSON diff ignores irrelevant whitespace and key order differences, showing only meaningful data changes. For configuration and data files, semantic diff is far more useful than line-level text diff.
Merging Changes
When two people edit the same file independently, a diff tool can identify whether their changes conflict. If person A changed line 10 and person B changed line 20, the changes can be merged automatically. If both changed line 10, there is a conflict that requires manual resolution.
Three-way merge — comparing the original, person A's version, and person B's version — is the algorithm used by version control systems. Understanding how merge conflicts arise and what the diff shows in a conflicted file is essential knowledge for anyone working in collaborative environments.