git diff
Inspecting changes with git diff — comparing working tree, staged, and committed states.
What you'll learn
- How to see unstaged, staged, and committed changes
- Comparing between branches and commits
- Reading the diff output format
- Using --stat for a summary of changes
Concept
What git diff Does
git diff shows the differences between various states of your repository. It is the answer to "what changed?" at every stage of the Git workflow.
git diff
With no arguments, it shows unstaged changes — modifications in your working directory that have not been git add-ed.
The Three Diff Views
git diff # unstaged changes (working tree vs staging area)
git diff --staged # staged changes (staging area vs last commit)
git diff HEAD # all changes (working tree vs last commit)
These three commands cover the most common questions:
- "What have I changed but not staged?" →
git diff - "What have I staged but not committed?" →
git diff --staged - "What will be different if I commit now?" →
git diff HEAD
Reading the Diff Output
diff --git a/login.js b/login.js
index 1a2b3c4..5d6e7f8 100644
--- a/login.js
+++ b/login.js
@@ -10,5 +10,8 @@ function login(user) {
const session = createSession(user);
- return session;
+ if (session.isValid) {
+ return session;
+ }
+ throw new Error('Invalid session');
}
--- a/fileand+++ b/file— the old and new versions@@ -10,5 +10,8 @@— the line numbers (old file lines 10-14, new file lines 10-17)- Lines starting with
-are removed (red) - Lines starting with
+are added (green) - Lines with no prefix are context (unchanged)
Comparing Branches and Commits
git diff main feature-login # what differs between two branches
git diff HEAD~1 HEAD # what changed in the last commit
git diff abc1234 def5678 # what changed between two commits
git diff main..feature-login # same as above (two-dot)
git diff main...feature-login # changes on feature-login since it diverged from main
The three-dot (...) syntax is particularly useful: it shows only the changes made on the feature branch, not changes that happened on main since the branch point.
The --stat Summary
For a quick overview without the full diff:
git diff --stat
login.js | 5 +++--
style.css | 2 ++
README.md | 8 ++++++++
3 files changed, 13 insertions(+), 2 deletions(-)
This shows which files changed and how many lines were added or removed — perfect for a quick review.
Word-Level Diffs
For long lines with small changes, use word-level diffing:
git diff --word-diff
git diff --color-words
This highlights changed words within a line instead of showing the entire old and new line.
Example
# See unstaged changes (what you changed but haven't staged)
echo "new line" >> README.md
git diff
# Stage the file and see staged changes
git add README.md
git diff # now shows nothing (no unstaged changes)
git diff --staged # shows the staged changes
git diff --cached # same as --staged (older name)
# See all changes (staged + unstaged) vs last commit
git diff HEAD
# Compare two branches
git diff main feature-login
# See what changed in the last commit
git diff HEAD~1 HEAD
# Summary of changes (file-level, no content)
git diff --stat
git diff --staged --stat
# Compare a specific file between branches
git diff main feature-login -- src/login.js
# See changes on feature branch since it diverged from main
git diff main...feature-login --stat
# Word-level diff for small changes in long lines
git diff --word-diff
# See what a specific commit changed
git show --stat abc1234
Covers the three essential diff views (unstaged, staged, HEAD), comparing branches and commits, the --stat summary, file-specific diffs, the three-dot syntax for branch divergence, and word-level diffs. The --stat flag is the quickest way to see what files changed.
Try it
- Code Diff
Compare two versions of code visually — same concept as git diff.
- File Hash Checksum
Git uses content hashes to detect changes — see how hashing works.
Common mistakes
The mistake
Running git diff and seeing nothing after you staged your changes
The fix
git diff shows only unstaged changes. After git add, your changes are staged and git diff shows nothing. Use git diff --staged (or git diff --cached) to see staged changes, or git diff HEAD to see all changes versus the last commit.
The mistake
Confusing the two-dot and three-dot diff syntax
The fix
git diff A..B shows the difference between the tips of A and B (all changes on both sides). git diff A...B shows only the changes on B since it diverged from A. The three-dot is usually what you want when reviewing a feature branch.
Related Resources
Related Snippets
- Git Log Snippet
git log and git diff work together to explore history.
Related Cheatsheets
- Git Cheatsheet
diff flags and comparison commands.
Practice git diff
3 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.