Progress:
TIER 3 · MODULE 24· Expert

git diff-index

What git diff --cached is built on.

🎯 What & why

git diff-index compares the index (or working tree) against an arbitrary tree-ish, usually a commit. It's what powers 'git diff --cached' and 'git diff HEAD'. If you need 'has anything changed since this commit?', this is the tool.

🧠 Mental model

Pick a tree on the left (any commit/tag/tree SHA), and either the index or the working tree on the right. Same output language as diff-files; just a different left-hand side.

🛠️ Synopsis

git diff-index [--cached] [--merge-base] [-p] [--raw] [--name-only] [-q]
               <tree-ish> [<path>...]

🎚️ Switches & options

FlagWhat it does
<tree-ish>The committed/tagged/tree SHA on the left side. Required.
--cachedCompare the index (not the working tree) to <tree-ish>. This is 'git diff --cached'.
--merge-baseUse the merge-base of HEAD and <tree-ish> as the left side instead.
-pProduce a unified patch.
--rawMachine-readable mode/SHA/status output (default-ish).
--name-onlyOnly print changed paths, one per line. Great for piping.
-q / --quietSilent; exit non-zero if any difference exists.

💡 Use cases

🧪 Examples

Working tree vs HEAD (patch)
git diff-index -p HEAD
Staged vs HEAD (porcelain's --cached)
git diff-index --cached -p HEAD
Names changed since v1.0
git diff-index --name-only v1.0
Clean-tree gate in a hook
git diff-index --quiet HEAD || exit 1

🎓 Recommendations

🪤 Common pitfalls

🔗 Related modules

📝 Quiz

Hit each option, then Check answers. Score is recorded; Next is always open.