Text Diff
Compare two texts side by side and highlight line-by-line differences
Text A
Text B
What is Text Diff?
A text diff (short for "difference") is the result of comparing two blocks of text and identifying which lines were added, removed, or left unchanged. The concept originates from the Unix diff utility, first released in 1974 as part of Version 5 Unix. Today, text diff is the backbone of version control systems like Git, where every commit stores a diff rather than a full copy of each file.
A diff algorithm finds the Longest Common Subsequence (LCS) between two sequences of lines. Lines present in the LCS are marked as unchanged. Lines in the original text but not the LCS are marked as removed. Lines in the modified text but not the LCS are marked as added. The result is a minimal set of changes needed to transform one text into the other.
Diff output comes in several formats. Unified diff (the default for git diff) prefixes removed lines with a minus sign and added lines with a plus sign. Side-by-side diff arranges both texts in parallel columns. This tool uses line-by-line comparison with color-coded output: green for additions, red for removals, and neutral for unchanged lines. Unchanged lines are shown with no prefix by default but can be hidden to focus only on what changed.
Why Use an Online Text Diff Tool?
Comparing text in a terminal requires installing diff utilities and dealing with command-line flags. A browser-based diff tool removes that friction entirely.
Text Diff Use Cases
Diff Output Formats Compared
Diff tools produce output in several formats. The table below summarizes the most common ones, what generates them, and when each is useful.
| Format | Tool / Source | Description |
|---|---|---|
| Unified diff | diff -u / git diff | Prefixes lines with + / - / space; includes @@ hunk headers |
| Side-by-side | diff -y / sdiff | Two columns, changed lines aligned horizontally |
| Context diff | diff -c | Shows changed lines with surrounding context, marked with ! / + / - |
| HTML diff | Python difflib | Color-coded HTML table with inline change highlights |
| JSON Patch | RFC 6902 | Array of add/remove/replace operations on a JSON document |
How Line Diff Works: The LCS Algorithm
Most line-diff tools, including this one, use the Longest Common Subsequence (LCS) algorithm. LCS finds the largest set of lines that appear in both texts in the same relative order, without requiring them to be contiguous. The lines not in the LCS are the actual differences.
The standard LCS algorithm uses dynamic programming and runs in O(m x n) time, where m and n are the line counts of the two texts. For large files, optimized variants like Myers' diff algorithm (used by Git) reduce this to O(n + d^2) where d is the number of differences, making it fast when most lines are shared.
Code Examples
Implementations of line-by-line text comparison in JavaScript, Python, Go, and the command line. Each example produces unified-style diff output.
// Line-by-line diff using the LCS algorithm
function diffLines(a, b) {
const linesA = a.split('\n')
const linesB = b.split('\n')
// Build LCS table
const m = linesA.length, n = linesB.length
const dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0))
for (let i = 1; i <= m; i++)
for (let j = 1; j <= n; j++)
dp[i][j] = linesA[i-1] === linesB[j-1]
? dp[i-1][j-1] + 1
: Math.max(dp[i-1][j], dp[i][j-1])
// Backtrack to produce diff
const result = []
let i = m, j = n
while (i > 0 || j > 0) {
if (i > 0 && j > 0 && linesA[i-1] === linesB[j-1]) {
result.unshift({ type: 'equal', text: linesA[i-1] }); i--; j--
} else if (j > 0 && (i === 0 || dp[i][j-1] >= dp[i-1][j])) {
result.unshift({ type: 'add', text: linesB[j-1] }); j--
} else {
result.unshift({ type: 'remove', text: linesA[i-1] }); i--
}
}
return result
}
const diff = diffLines("alpha\nbeta\ngamma", "alpha\nbeta changed\ngamma\ndelta")
// → [
// { type: 'equal', text: 'alpha' },
// { type: 'remove', text: 'beta' },
// { type: 'add', text: 'beta changed' },
// { type: 'equal', text: 'gamma' },
// { type: 'add', text: 'delta' }
// ]import difflib
text_a = """alpha
beta
gamma""".splitlines()
text_b = """alpha
beta changed
gamma
delta""".splitlines()
# Unified diff (same format as git diff)
for line in difflib.unified_diff(text_a, text_b, fromfile='a.txt', tofile='b.txt', lineterm=''):
print(line)
# --- a.txt
# +++ b.txt
# @@ -1,3 +1,4 @@
# alpha
# -beta
# +beta changed
# gamma
# +delta
# HTML side-by-side diff
d = difflib.HtmlDiff()
html = d.make_file(text_a, text_b, fromdesc='Original', todesc='Modified')package main
import (
"fmt"
"strings"
)
// Minimal LCS-based line diff
func diffLines(a, b string) {
la := strings.Split(a, "\n")
lb := strings.Split(b, "\n")
m, n := len(la), len(lb)
dp := make([][]int, m+1)
for i := range dp {
dp[i] = make([]int, n+1)
}
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
if la[i-1] == lb[j-1] {
dp[i][j] = dp[i-1][j-1] + 1
} else if dp[i-1][j] >= dp[i][j-1] {
dp[i][j] = dp[i-1][j]
} else {
dp[i][j] = dp[i][j-1]
}
}
}
var result []string
i, j := m, n
for i > 0 || j > 0 {
if i > 0 && j > 0 && la[i-1] == lb[j-1] {
result = append([]string{" " + la[i-1]}, result...)
i--; j--
} else if j > 0 && (i == 0 || dp[i][j-1] >= dp[i-1][j]) {
result = append([]string{"+" + lb[j-1]}, result...)
j--
} else {
result = append([]string{"-" + la[i-1]}, result...)
i--
}
}
for _, line := range result {
fmt.Println(line)
}
}
// Output:
// alpha
// -beta
// +beta changed
// gamma
// +delta# Compare two files with unified diff (3 lines of context) diff -u original.txt modified.txt # Git diff between working tree and last commit git diff HEAD -- file.txt # Git diff between two branches git diff main..feature -- src/ # Side-by-side diff in the terminal diff -y --width=120 original.txt modified.txt # Color-coded diff (requires colordiff) diff -u original.txt modified.txt | colordiff