Skip to content

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:

echo "line 2" >> file.txt
git add file.txt
echo "line 3" >> file.txt

Show unstaged changes (working directory vs staging area):

git diff

Show staged changes (staging area vs last commit):

git diff --staged

Show all changes (working directory vs last commit):

git diff HEAD

Diff a specific file:

git diff file.txt
git diff --staged file.txt

Show word-level diff instead of line-level:

git diff --word-diff

Show only file names that changed:

git diff --name-only
git diff --name-status  # includes A/M/D status

Diff between two commits:

git diff HEAD~2 HEAD

Diff between branches:

git diff main..feature

Show stat summary:

git diff --stat HEAD~1

What to Look For

  • git diff (no args) shows only what is modified but NOT yet staged
  • git diff --staged shows only what IS staged and will go into the next commit
  • git diff HEAD shows everything that differs from the last commit
  • After git add, changes move from diff output to diff --staged output

Common Mistakes

  • Running git diff and seeing no output, assuming nothing changed (changes may be staged)
  • Confusing --staged with --cached (they are synonyms but some forget --staged exists)
  • Not understanding that git diff compares working tree to index, not to HEAD
  • Forgetting --name-only when you just need to know which files changed

Cleanup

rm -rf /tmp/diff-drill