Compare two texts or code snippets and view differences side-by-side
Text diff compares two pieces of text and highlights the differences between them. This is essential for code review, tracking changes, debugging, and version comparison. The diff shows exactly what was added, removed, or modified between two versions.
Diff algorithms find the longest common subsequence between texts, then mark non-matching sections as additions or deletions. Line-by-line diff is most common for code. Character-level diff shows changes within lines. Output formats include unified diff, side-by-side, and inline highlighting.
The Myers diff algorithm (used by git) finds the minimum edit distance efficiently. Unified diff format shows context lines around changes, with - for deletions and + for additions. Three-way diff compares against a common ancestor for merge conflict resolution.
Review changes between code versions.
Compare function before and after refactoringFind differences between config files across environments.
Track changes in documents and articles.
Compare expected vs actual output.
# Basic diff
diff file1.txt file2.txt
# Unified format (like git)
diff -u file1.txt file2.txt
# Ignore whitespace
diff -w file1.txt file2.txt
# Side by side
diff -y file1.txt file2.txt
# Git diff
git diff HEAD~1 -- file.jsUnix diff command is the standard tool. -u gives unified format, -y shows side by side.
Use diff when comparing any two versions of text: code changes, configuration differences, document revisions, or debugging output differences.
Line diff compares whole lines - any change marks the entire line as modified. Word/character diff shows exactly which words or characters changed within a line. Use line diff for code, word diff for prose.