patterngitTip
git diff --cached: see what's staged before committing
Viewed 0 times
git diff cachedstaged diffdiff stagedreview before commitwhat will be committed
Problem
Developers commit without reviewing staged changes, accidentally including debug lines, console.logs, or unintended edits that were staged with
git add .Solution
Always review staged changes before committing:
git diff --cached # same as: git diff --staged
This shows the diff between the index (staged area) and the last commit — exactly what will be in the next commit.
# Compare staged changes against a specific commit
git diff --cached main
# See only the file names that are staged
git diff --cached --name-only
# Stat summary of staged changes
git diff --cached --stat
git diff --cached # same as: git diff --staged
This shows the diff between the index (staged area) and the last commit — exactly what will be in the next commit.
# Compare staged changes against a specific commit
git diff --cached main
# See only the file names that are staged
git diff --cached --name-only
# Stat summary of staged changes
git diff --cached --stat
Why
git diff without flags shows unstaged changes (working tree vs index). git diff --cached shows staged changes (index vs HEAD). They are complementary views of what's modified vs what's committed.Gotchas
git statusshows which files are staged but not the actual diffs — always rungit diff --cachedfor a full review- In a large diff, pipe through a pager:
git diff --cached | less -R git diff HEADshows all changes (staged + unstaged) compared to last commit
Code Snippets
Reviewing staged changes before committing
# See what's staged (what will be committed)
git diff --cached
# Only file names
git diff --cached --name-only
# Summary of additions/deletions
git diff --cached --stat
# Everything changed (staged + unstaged) vs HEAD
git diff HEADContext
Before running git commit to verify exactly what will be included
Revisions (0)
No revisions yet.