Effective diff checking is critical for code reviews and debugging. Understanding how to read, interpret, and use diff tools dramatically improves development workflow.
This guide covers diff basics, advanced techniques, and best practices for code reviews.
Understanding Diff Output#
Unified Diff Format:
--- file1.txt
+++ file2.txt
@@ -1,3 +1,4 @@
unchanged line
-removed line
+added line
+another new line
unchanged line
Reading the Symbols:
-— Line was removed+— Line was added- No prefix — Line unchanged (context)
@@— Chunk header showing line ranges
Split View vs Unified:
- Unified: Shows changes inline (good for small changes)
- Split: Side-by-side (better for large refactors)
Git Diff Commands#
Essential Git Diff Commands:
# See unstaged changes
git diff
# See staged changes
git diff --staged
# Compare branches
git diff main..feature-branch
# Compare specific files
git diff HEAD~1 file.js
# Word-level diff (better for prose)
git diff --word-diff
# Ignore whitespace changes
git diff -w
Advanced Options:
--stat— Summary of changes--name-only— List changed files only--patience— Better algorithm for moved code
When to Use Online Diff Tools#
Use Online Tools When:
- Comparing config files outside version control
- Debugging JSON/XML responses
- Reviewing non-code text changes
- Quick ad-hoc comparisons
- Sharing diff with non-technical stakeholders
Use Git Diff When:
- Code is in version control
- Reviewing pull requests
- Comparing commit history
- Merge conflict resolution
Try our Diff Checker for quick comparisons.
Diff Algorithms Explained#
Myers Algorithm (Default):
Fast and works well for most cases. Minimizes number of edits.
Patience Algorithm:
Better for code that has been reorganized. Produces more intuitive diffs when code blocks are moved.
git diff --patience
Histogram Algorithm:
Like patience but faster. Good default for large files.
git diff --histogram
When to Switch: If git diff shows confusing changes for moved code, try --patience or --histogram.
Merge Conflict Resolution with Diff#
Understanding Conflict Markers:
<<<<<<< HEAD
Your current changes
=======
Incoming changes
>>>>>>> feature-branch
Resolution Strategy:
- Use
git diffto see what changed in both branches - Identify the actual conflict (logic vs formatting)
- Manually merge, keeping necessary parts from both
- Remove conflict markers
- Test thoroughly
- Commit the resolution
Tools: VSCode, GitKraken, KDiff3, Meld provide visual merge diff tools.
Code Review Best Practices#
Effective Diff-Based Code Review:
- Start with high-level:
git diff --statfor overview - Review file-by-file: Don't try to review 50 files at once
- Ignore formatting: Use
git diff -wto ignore whitespace - Focus on logic: Look for edge cases, null checks, performance
- Ask questions: Comment on unclear changes
- Approve incrementally: Break large PRs into smaller ones
Red Flags in Diffs:
- Large functions added without tests
- Security-sensitive code (auth, validation)
- Database schema changes
- Commented-out code
- TODO/FIXME comments
Frequently Asked Questions
What is the difference between git diff and diff command?▼
How do I ignore whitespace in diff?▼
What does @@ -1,3 +1,4 @@ mean in diff output?▼
Should I use unified or split diff view?▼
How do I compare files that are not in git?▼
Compare Files Side-by-Side
Use our free diff checker to compare text, code, or configuration files. Visual diff with syntax highlighting.