Git Diff to Compare Changes
Git diff is a powerful command for comparing changes in your repository. This guide will show you how to use Git diff effectively to review code changes, track modifications, and understand differences between versions.
What You’ll Learn
- Basic Git diff commands
- Different diff formats and options
- How to compare specific changes
- Visualizing differences
- Best practices for code review
Implementation Steps
-
Basic Diff Command
# Show unstaged changes git diff
- Shows changes in working directory
- Compares with staged changes
- Displays line-by-line differences
- Uses +/- to show additions/deletions
-
Staged Changes
# Show staged changes git diff --staged
- Shows changes ready to commit
- Compares with last commit
- Good for review before commit
- Helps catch mistakes
-
Compare Commits
# Compare two commits git diff commit1 commit2 # Compare with previous commit git diff HEAD~1
- Shows changes between commits
- Useful for code review
- Helps track feature changes
- Good for release notes
-
Compare Branches
# Compare branches git diff main..feature # Compare with remote git diff origin/main
- Shows branch differences
- Helps with merge planning
- Good for feature review
- Useful for conflict resolution
Best Practices
-
Diff Formatting
- Use
--color-words
for word-level changes - Use
--stat
for summary - Use
--name-only
for file list - Use
--name-status
for file status
- Use
-
Code Review
- Review changes before committing
- Check for unintended changes
- Look for sensitive data
- Verify formatting
-
Diff Tools
# Configure external diff tool git config --global diff.tool vscode git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
-
Diff Documentation
- Document significant changes
- Explain complex diffs
- Reference issue numbers
- Keep diffs focused
Common Use Cases
-
Reviewing Changes
# Show word-level changes git diff --color-words # Show file statistics git diff --stat
-
Comparing Files
# Compare specific files git diff file1.txt file2.txt # Compare file versions git diff HEAD~1:file.txt file.txt
-
Checking Staged Changes
# Show staged changes git diff --staged # Show staged file list git diff --staged --name-only
-
Branch Comparison
# Show branch differences git diff main...feature # Show file changes git diff main...feature -- file.txt
Advanced Usage
-
Custom Diff Format
# Show context lines git diff -U5 # Show function context git diff -W
-
Diff with Blame
# Show who changed each line git blame file.txt # Show commit info git log -p file.txt
-
Diff with Patches
# Create patch file git diff > changes.patch # Apply patch git apply changes.patch
-
Diff with Reflog
# Show all changes git diff HEAD@{1} # Show branch changes git diff HEAD@{1}...HEAD
Common Issues and Solutions
-
Large Diffs
# Show only file names git diff --name-only # Show only changed lines git diff --unified=0
-
Binary Files
# Show binary file changes git diff --text # Ignore binary files git diff --diff-filter=AM
-
Whitespace Changes
# Ignore whitespace git diff -w # Show only whitespace git diff --check
Conclusion
Git diff is an essential tool for code review and change tracking. Remember to:
- Use appropriate diff formats
- Review changes carefully
- Document significant changes
- Keep diffs focused
- Use diff tools effectively
Next Steps
After mastering Git diff, you might want to:
- Learn about Git merge
- Explore Git rebase
- Study Git hooks
- Understand Git internals