git diff Command Guide
Show changes between commits, commit and working tree, etc
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
git diffWorking dir vs stagedgit diff --stagedStaged vs last commitgit diff HEADWorking dir vs HEADCompare
git diff <branch>Compare with branchgit diff a..bBetween commitsgit diff a...bCommon ancestor to bOptions
--statSummary statistics--name-onlyFile names only-wIgnore whitespaceDownloadable Image Preview
Basic Usage
The git diff command shows differences between various states of your repository - working directory, staging area, and commits.
# Show unstaged changes
git diff
# Show staged changes (ready for commit)
git diff --staged
git diff --cached # Same as --staged
# Show all changes (staged + unstaged)
git diff HEADComparing Different States
# Working directory vs staging area
git diff
# Staging area vs last commit
git diff --staged
# Working directory vs last commit
git diff HEAD
# Working directory vs specific commit
git diff abc1234
# Between two commits
git diff abc1234 def5678
git diff abc1234..def5678 # Same thing
# Specific file
git diff -- file.js
git diff HEAD -- file.jsUnderstanding Diff States
- Working Directory: Your current file changes
- Staging Area (Index): Changes added with git add
- HEAD: Last commit on current branch
Common Options
| Option | Description |
|---|---|
| --stat | Show diffstat summary |
| --name-only | Show only changed file names |
| --name-status | Show names and change status (A/M/D) |
| -w, --ignore-all-space | Ignore all whitespace |
| --word-diff | Show word-level diff |
| --color-words | Highlight changed words in color |
Diffing Branches
# Changes between branches
git diff main..feature
# What's in feature that's not in main
git diff main...feature
# Compare with remote branch
git diff origin/main
# Before merging, see what would change
git diff HEAD...origin/main
# Compare specific file across branches
git diff main..feature -- src/app.jsPractical Examples
# Review before committing
git diff --staged
# Quick summary of changes
git diff --stat
# See what files changed
git diff --name-only HEAD~3
# Ignore whitespace changes
git diff -w
# Word-level diff (useful for prose)
git diff --word-diff
# Compare with last week
git diff HEAD@{1.week.ago}
# Export diff as patch
git diff > changes.patch
# Check if there are changes
git diff --quiet || echo "Changes detected"Frequently Asked Questions
What is the difference between git diff and git diff --staged?
"git diff" shows unstaged changes (working directory vs staging area). "git diff --staged" (or --cached) shows staged changes that will go into the next commit.
How do I see changes between two branches?
Use "git diff branch1..branch2" to see changes. "git diff main..feature" shows what feature has that main doesn't.
How do I diff a specific file?
Add the filename at the end: "git diff HEAD -- filename" or "git diff branch1..branch2 -- filename".
How do I ignore whitespace changes?
Use "git diff -w" or "--ignore-all-space". Use "-b" to ignore changes in amount of whitespace.
What does the diff output format mean?
Lines starting with - are removed, + are added. @@ shows the line numbers. The header shows the files being compared.
Summary
git diff is essential for reviewing changes before committing. Use --staged to see what's going into your next commit.
Quick Reference
git diff- Unstaged changesgit diff --staged- Staged changesgit diff HEAD- All changesgit diff branch1..branch2- Between branches
Official Documentation
For authoritative information, refer to the official documentation: