What is Diff?
Diff is a comparison technique that identifies and displays the differences between two sets of data, typically text files or code, showing what has been added, removed, or modified.
Quick Facts
| Full Name | Difference / Diff Algorithm |
|---|---|
| Created | 1974 (Unix diff utility) |
| Specification | Official Specification |
How It Works
Diff (short for 'difference') compares two versions of text or data and outputs the changes between them. The diff algorithm, developed for Unix in the 1970s, uses longest common subsequence (LCS) algorithms to efficiently identify minimal edits. Output typically shows added lines (prefixed with +), removed lines (prefixed with -), and unchanged context lines. Diff is fundamental to version control systems like Git, where it tracks changes between commits. Modern diff tools offer side-by-side comparison, syntax highlighting, word-level diff, and three-way merge capabilities. Beyond text, diff concepts apply to JSON comparison, database schema changes, and binary file comparison. Common diff formats include unified diff (most common), context diff (older format), and side-by-side diff. Understanding diff output is essential for code review, debugging, and collaborative development.
Key Characteristics
- Compares two versions of text or data
- Shows additions (+), deletions (-), and changes
- Uses efficient algorithms (LCS-based)
- Produces minimal edit sequences
- Foundation of version control systems
- Multiple output formats available
Common Use Cases
- Code review and pull request reviews
- Version control (Git diff, SVN diff)
- Configuration file comparison
- Document revision tracking
- Database migration and schema comparison
Example
Loading code...Frequently Asked Questions
What do the symbols +, -, and @@ mean in a diff output?
In unified diff format: '+' indicates lines that were added, '-' indicates lines that were removed, lines without prefix are context (unchanged). The '@@' markers show line numbers: @@ -old_start,old_count +new_start,new_count @@ indicates which lines in the old and new files are being compared.
What is the difference between unified diff and side-by-side diff?
Unified diff shows changes in a compact format with + and - prefixes, combining both versions in a single view. Side-by-side diff displays the old and new versions in parallel columns, making it easier to visually compare but using more screen space. Most code review tools use unified diff, while GUI diff tools often offer side-by-side views.
How can I generate a diff between two files or directories?
For files: 'diff file1.txt file2.txt' or 'diff -u file1.txt file2.txt' for unified format. For directories: 'diff -r dir1 dir2' compares recursively. In Git: 'git diff' shows unstaged changes, 'git diff --staged' shows staged changes, 'git diff commit1 commit2' compares commits.
What is a three-way merge and when is it used?
A three-way merge compares two modified versions against their common ancestor to automatically resolve changes. It's used when merging branches in version control. If both versions changed the same lines differently, a merge conflict occurs and requires manual resolution. Tools like git mergetool help visualize and resolve these conflicts.
How do I apply a diff patch to update files?
Use the 'patch' command: 'patch < changes.diff' applies the diff to files in the current directory. Options include: '-p1' to strip directory prefixes, '--dry-run' to preview changes without applying, '-R' to reverse/undo a patch. In Git, 'git apply patch.diff' or 'git am' for email-formatted patches.