DevFormatter

Text Diff

Text A (original)
Text B (modified)
Diff Output

A text diff tool compares two versions of a text line by line and shows exactly which lines were added, removed, or unchanged between them. It uses the Myers diff algorithm — the same algorithm used by Git, GNU diff, and most version control systems — to find the shortest possible edit script transforming the original text into the modified version.

How it works

A text diff compares two versions of a text and shows exactly which lines were added, removed, or unchanged. Diff tools are essential for reviewing code changes, comparing configuration files, checking document revisions, and understanding what changed between two versions of any text.

This tool implements the Myers diff algorithm (Eugene Myers, 1986) — the same algorithm used by Git, GNU diff, and most version control systems. Myers diff finds the shortest edit script: the minimum number of line insertions and deletions needed to transform Text A into Text B.

+ green lines are insertions (in B, not in A). - red lines are deletions (in A, not in B). Unchanged lines appear in grey.

Last updated: November 2026

Unified Diff Format Reference

Prefix Meaning Example
+Line added in Text B+ const x = 2;
-Line removed from Text A- const x = 1;
(space)Unchanged context line function foo() {
@@Hunk header (line ranges)@@ -1,3 +1,4 @@
---Original file marker--- v1.txt
+++Modified file marker+++ v2.txt

Tips & Tricks

A modified line = delete + insert

Myers diff works at the line level. A line that changed appears as one red deletion immediately followed by one green insertion. There is no "modified" state — only add and remove.

Format JSON before diffing

If two JSON objects have the same data but different formatting, this tool will show every line as changed. Format both inputs to the same indentation first, or use the JSON Diff tool for a structure-aware comparison.

Paste git diff output directly

The output format matches unified diff — compatible with git apply and patch. You can copy the diff output and apply it with: patch original.txt < changes.patch

Trailing newlines matter

A file ending with a newline and one without will show a difference on the last line. This is a real difference — most text editors and linters enforce a trailing newline (POSIX standard for text files).

Use Swap A↔B to reverse a diff

The Swap button exchanges Text A and Text B. Insertions become deletions and vice versa. Useful when you want to see what lines you removed rather than what was added.

Large files diff in milliseconds

Myers diff runs in O((N+M)D) time where D is the edit distance. For typical files (thousands of lines, small diff), this is effectively instant in the browser. For 10K+ line files with many changes, expect a brief pause.

Code Examples

Node.js — diff package

const Diff = require('diff');
const fs   = require('fs');

const a = fs.readFileSync('v1.txt', 'utf8');
const b = fs.readFileSync('v2.txt', 'utf8');

const diff = Diff.diffLines(a, b);
diff.forEach(part => {
  const prefix = part.added ? '+ ' : part.removed ? '- ' : '  ';
  process.stdout.write(prefix + part.value);
});

Python — difflib

import difflib

with open('v1.txt') as f: a = f.readlines()
with open('v2.txt') as f: b = f.readlines()

# Unified diff (same format as git diff)
diff = difflib.unified_diff(a, b, fromfile='v1.txt', tofile='v2.txt')
print(''.join(diff))

# HTML diff (colour-coded, viewable in a browser)
html = difflib.HtmlDiff().make_file(a, b)
with open('diff.html', 'w') as f: f.write(html)

CLI — GNU diff

# Standard diff (line-by-line)
diff v1.txt v2.txt

# Unified diff (Git-style, with context lines)
diff -u v1.txt v2.txt

# Side-by-side diff
diff -y v1.txt v2.txt

# Ignore whitespace changes
diff -b v1.txt v2.txt

# Recursive diff of two directories
diff -r dir1/ dir2/

# Git diff between commits
git diff HEAD~1 HEAD -- file.txt

Frequently Asked Questions

What is a text diff?

A text diff compares two versions of a text line by line and shows exactly which lines were added, removed, or unchanged. Diff tools are used by developers to review code changes, compare config files, check document revisions, and understand what changed between any two versions of text. The output uses a colour-coded format: green lines (marked with +) are insertions present only in the new version, red lines (marked with -) are deletions present only in the original, and grey unmarked lines are unchanged context that helps locate where in the file the changes occurred. Text diff is the foundation of every code review workflow — GitHub pull request views, GitLab merge requests, and Gerrit all render a diff of this form. The tool is format-agnostic: it works equally well on source code, Markdown documents, YAML configuration files, SQL queries, or any other plain-text content.

What algorithm does this tool use?

This tool uses the Myers diff algorithm, published by Eugene Myers in 1986. Myers diff finds the shortest edit script — the minimum number of line insertions and deletions needed to transform text A into text B. It is the same algorithm used by Git, GNU diff, and most version control systems. The algorithm runs in O((N+M)D) time and O(N+M) space, where N and M are the lengths of the two texts and D is the edit distance (number of insertions plus deletions). For typical code review scenarios — files of a few hundred to a few thousand lines with small diffs — this is effectively instant. Myers published the algorithm in the paper "An O(ND) Difference Algorithm and Its Variations" in Algorithmica (1986). Git also offers the patience and histogram diff strategies as alternatives for cases where Myers produces noisy output on certain code patterns, but Myers remains the default.

What do the colours mean?

Green lines (marked +) are insertions — lines present in Text B but not Text A. Red lines (marked -) are deletions — lines in Text A but not Text B. Unchanged lines appear in grey. A modified line appears as one deletion followed by one insertion. This colour scheme follows the universal convention established by GNU diff and used by Git, GitHub, GitLab, and virtually every other diff tool: red for removed, green for added. The unchanged grey lines are called context lines — they provide surrounding code so you can understand what function or block the change is part of, without having to open the full file. The combined insertion and deletion count displayed in the toolbar represents the edit distance between the two texts — a useful single-number summary of how different the two versions are.

What is the difference between a line diff and a word diff?

A line diff compares texts line by line — the whole line is marked added or removed if anything in it changed. A word diff highlights the specific words or characters within a changed line that differ. This tool performs a line diff, which is standard for code and config file comparison. For example, if you change a single character in the middle of a long line, a line diff marks the entire line as deleted and re-added, making it harder to spot the exact change. A word diff (available in Git with git diff --word-diff) narrows it down to the changed word. For prose documents, word diff is often more useful; for code, line diff is standard because whole-line changes are the meaningful unit of code modification. GitHub offers character-level diff highlighting within changed lines in pull requests, combining the best of both approaches.

How do I compare files using the terminal?

Use diff file1.txt file2.txt for a standard diff. For unified diff (Git-style): diff -u file1.txt file2.txt. The output uses --- for the original, +++ for the new file, - for removed lines, + for added lines, and no prefix for context lines. Additional useful options: diff -i ignores case differences; diff -b ignores changes in whitespace amount; diff -B ignores blank line differences; diff -y produces a side-by-side comparison. To compare two directories recursively, use diff -r dir1/ dir2/. On macOS, the system diff is BSD diff; install GNU diffutils via Homebrew (brew install diffutils) to get the richer gdiff command with all GNU options. For coloured terminal output, use colordiff (brew install colordiff or apt install colordiff).

What is a unified diff format?

Unified diff is the standard patch format used by Git and GNU diff. It prefixes removed lines with -, added lines with +, and shows context lines around each change. Hunk headers like @@ -1,3 +1,4 @@ indicate the starting line and line count in each file. This format is used in .patch files and git diff output. Reading a hunk header: @@ -1,3 +1,4 @@ means the hunk starts at line 1 of the original file and covers 3 lines (-1,3), and starts at line 1 of the new file and covers 4 lines (+1,4) — the difference in line counts (3 vs 4) tells you one line was added. Unified diff files (usually with a .patch extension) can be applied to a codebase using the patch command: patch -p1 < changes.patch, or applied via Git: git apply changes.patch. GitHub pull requests and email-based code review workflows (Linux kernel, Git itself) both use this format.

Is this tool useful for comparing JSON or YAML?

Yes, but for structured data, a semantic diff is more useful. This tool compares raw text lines — whitespace and key ordering affect the output. For JSON specifically, the JSON Diff tool on this site compares parsed data structures and ignores formatting differences. A practical workaround when you do want to use text diff for JSON: format both inputs to canonical form first (same indentation, sorted keys) so whitespace differences are eliminated before comparing. With jq: jq -S . file.json sorts keys (-S) and pretty-prints, normalising the format. Then text-diffing the two normalised outputs will show only real data changes. For YAML, use a YAML formatter to normalise both inputs to consistent indentation and quoting before text diffing. For large structured files in CI pipelines, semantic diff tools are always preferable because they handle format normalisation automatically.

How do I diff two files in Git?

Use git diff to compare working tree vs last commit. To compare two commits: git diff abc123 def456 -- file.txt. To compare two branches: git diff main..feature. To see only changed file names: git diff --name-only. Git uses Myers algorithm for all these comparisons. Some additional useful Git diff options: git diff --stat shows a summary of changed files and line counts without the full diff; git diff --cached (or --staged) shows the diff of changes already staged with git add; git diff -w ignores all whitespace changes, useful when re-indenting code; git diff --word-diff shows changes within lines at the word level. To permanently switch a repository's diff algorithm to histogram (which often produces cleaner output on refactored code): git config diff.algorithm histogram.

Is my text sent to a server?

No. All diffing runs entirely in your browser using JavaScript. No text is uploaded, stored, or logged. You can safely compare private code, configuration files, or sensitive documents. The Myers diff algorithm is implemented as a pure TypeScript function that takes two strings and returns a diff result — no network requests are made during or after the comparison. You can verify this by opening your browser's DevTools Network tab while pasting text — zero outbound requests will be triggered. This makes the tool suitable for comparing internal Kubernetes manifests, Terraform plans, database schema migrations, or any other sensitive infrastructure configuration that should not leave your environment.

How do I compare two strings in Python?

Use the built-in difflib module: import difflib; diff = difflib.unified_diff(a.splitlines(), b.splitlines(), lineterm=""); print("\n".join(diff)). difflib.SequenceMatcher uses an algorithm similar to Myers diff and is available in Python's standard library. For human-readable output, difflib.Differ produces a format closer to what you see in this tool: d = difflib.Differ(); result = list(d.compare(a.splitlines(), b.splitlines())). Lines prefixed with "- " are in the first text only, "+ " are in the second only, " " (two spaces) are common to both, and "? " lines (optional) show which characters changed within a line. difflib.HtmlDiff().make_file(a_lines, b_lines) generates a full side-by-side HTML diff viewable in any browser — useful for generating diff reports in CI pipelines.

How do I compare two strings in JavaScript / Node.js?

Use the diff npm package: npm install diff then const Diff = require("diff"); const result = Diff.diffLines(textA, textB); result.forEach(p => console.log((p.added ? "+" : p.removed ? "-" : " ") + p.value)). The diff package uses Myers algorithm and is also used internally by Jest for assertion diffs. The package exposes multiple diff granularities: Diff.diffWords() compares word by word, Diff.diffChars() compares character by character, and Diff.diffLines() compares line by line. Each part in the result array has added, removed (booleans), and value (string). For generating a unified diff string compatible with the patch command: const { createTwoFilesPatch } = require("diff"); const patch = createTwoFilesPatch("v1.txt", "v2.txt", textA, textB). The diff package is one of the most widely downloaded packages on npm with over 50 million weekly downloads.

What is edit distance?

Edit distance (Levenshtein for characters, or line insertion/deletion count for a line diff) measures how many operations are needed to transform one text into another. Myers diff computes the minimum line edit distance. The insertion and deletion counts shown after diffing represent the edit distance between the two texts. Levenshtein distance counts character-level insertions, deletions, and substitutions. Myers diff counts line-level insertions and deletions only (no substitutions — a changed line is modelled as one deletion plus one insertion). A lower edit distance generally means the two versions are more similar, though the exact number depends on how the texts are split into lines. In testing and fuzzing, edit distance is used to measure how different two output versions are — a sudden jump in edit distance between consecutive program outputs can indicate a regression. The Python difflib.SequenceMatcher().ratio() method returns a float between 0 (completely different) and 1 (identical) based on the edit distance.

Related Tools