Viewing Changes
git diff shows what changed between different states of your repository. It’s essential for reviewing changes before committing.
Unstaged Changes
See changes not yet staged:
git diff
# diff --git a/file.txt b/file.txt
# --- a/file.txt
# +++ b/file.txt
# @@ -1 +1 @@
# -old line
# +new line
Staged Changes
See changes that are staged for commit:
git diff --staged
All Changes
See everything (staged and unstaged):
git diff HEAD
Statistical Summary
Get a quick overview of what changed:
git diff --stat
# file.txt | 2 +-
# 1 file changed, 1 insertion(+), 1 deletion(-)
Reading Diff Output
-lines show what was removed+lines show what was added@@shows the line range affected- Colors help distinguish additions (green) from deletions (red)
Best Practices
- Review changes with
git diffbefore staging - Use
git diff --stagedbefore committing - Use
git diff --statfor quick overviews - Compare specific commits with
git diff commit1 commit2
Common Mistakes
- Not reviewing changes before committing
- Confusing staged and unstaged diffs
- Ignoring whitespace changes that affect functionality
- Not using diff to understand merge conflicts