Q11 of 40 · Git
What does `git diff` show by default?
GitJuniorgitdiffstaging-areafundamentalscomparison
Short answer
Short answer: git diff with no arguments shows unstaged changes — the difference between the working directory and the staging area. git diff --staged shows staged changes versus the last commit. git diff HEAD shows all changes (staged and unstaged) versus the last commit. These three commands cover the three areas of Git state.
Detail
The three forms of git diff map to Git's three areas:
| Command | Compares | What you see |
|---|---|---|
git diff |
working dir vs staging area | unstaged changes |
git diff --staged |
staging area vs last commit | what next commit will include |
git diff HEAD |
working dir vs last commit | all pending changes |
Comparing across commits and branches:
git diff main..feature/login— all changes between two branchesgit diff HEAD~3 HEAD— changes over the last 3 commitsgit diff a1b2c3d e4f5678— between two specific commits
Limiting to specific paths:
git diff -- tests/ # only show diffs within the tests/ directory
Stat view (no line-level diff, just counts):
git diff --stat HEAD # "3 files changed, 42 insertions(+), 5 deletions(-)"
A common interview question traps: git diff after git add shows nothing — because the changes are now staged, moving them out of the unstaged zone. Use git diff --staged to see them.
// EXAMPLE
# See unstaged changes (working directory vs staging area)
git diff
# See staged changes (staging area vs last commit)
git diff --staged
# See ALL changes since last commit
git diff HEAD
# After git add — this shows nothing (changes are now staged)
git add tests/UserApiTest.java
git diff # empty
git diff --staged # shows the staged change
# Branch comparison — what feature/login adds over main
git diff main..feature/login
# Summary of changes (no line-level details)
git diff --stat HEAD// WHAT INTERVIEWERS LOOK FOR
Knowing the three forms and which area each compares. The trap — that git diff shows nothing after git add — reveals whether the candidate understands the staging area. The branch-comparison syntax is a bonus.
// COMMON PITFALL
Running git diff and seeing nothing, then assuming there are no changes, when in fact all changes are staged. Always check git status to understand which area to diff against.