Drill: Understanding git diff Variants¶
Goal¶
Understand the difference between git diff, git diff --staged, and git diff HEAD to inspect changes at each stage.
Setup¶
- A git repository with some committed files
Commands¶
Set up a practice scenario:
mkdir /tmp/diff-drill && cd /tmp/diff-drill && git init
echo "line 1" > file.txt && git add file.txt && git commit -m "initial"
Make changes and stage some:
Show unstaged changes (working directory vs staging area):
Show staged changes (staging area vs last commit):
Show all changes (working directory vs last commit):
Diff a specific file:
Show word-level diff instead of line-level:
Show only file names that changed:
Diff between two commits:
Diff between branches:
Show stat summary:
What to Look For¶
git diff(no args) shows only what is modified but NOT yet stagedgit diff --stagedshows only what IS staged and will go into the next commitgit diff HEADshows everything that differs from the last commit- After
git add, changes move fromdiffoutput todiff --stagedoutput
Common Mistakes¶
- Running
git diffand seeing no output, assuming nothing changed (changes may be staged) - Confusing
--stagedwith--cached(they are synonyms but some forget--stagedexists) - Not understanding that
git diffcompares working tree to index, not to HEAD - Forgetting
--name-onlywhen you just need to know which files changed