Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Inspecting History

git diff-index command

Reference for git diff-index: compare a tree with the index (--cached) or the working tree, read the raw output and zero IDs, and use --quiet for scripts.

git diff-index compares a tree (usually the tree of a commit such as HEAD) with either the index or the working tree and prints the differences in the stable raw format. It is the plumbing counterpart of git diff HEAD and git diff --cached, and is what shell scripts use to ask "is there anything uncommitted?".

Without --cached it compares the tree with the working tree, looking only at files that are in the index. With --cached it compares the tree with the index and does not touch the working tree at all, which makes it fast and independent of file timestamps.

Terminal
git diff-index [--cached] <tree-ish> [-- <path>...]
git diff-index --quiet HEAD --

Try it

Bash Working tree and index against HEAD

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Scripting with --quiet

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git diff-index [-m] [--cached] [--merge-base] [<diff-options>] <tree-ish> [--] [<path>...]
Name Description
<tree-ish> The tree to compare against, normally HEAD.
--cached Compare the tree with the index and ignore the working tree.
-m Treat files that exist in the index but are missing in the working tree as unchanged, instead of deleted.
--merge-base Compare against the merge base of the given commit and HEAD.
--quiet Print nothing and exit with 1 if there are differences (implies --exit-code).
<path>... Limit to paths.
<diff-options> --name-only, --name-status, -p, --raw, --abbrev, -M and others.

Output. The default is the raw format (one line per changed file):

Text
:<old mode> <new mode> <old id> <new id> <status><TAB><path>

For comparisons with the working tree the new ID is shown as all zeros when the file content has not been stored as an object yet. That is normal: it means "the working tree version", not an error.

Returns: exit status 0, or 1 with --quiet/--exit-code when differences exist.

Usage notes

  • Only tracked files are compared. An untracked file is invisible to diff-index, as it is to git diff.
  • Stale index data. Plumbing commands do not refresh the index, so files that were touched without a content change can be reported as modified. Scripts should run git update-index -q --refresh first; porcelain commands do this for you.
  • A clean-tree test: git update-index -q --refresh && git diff-index --quiet HEAD -- is the classic idiom. Include the trailing -- so a branch and a file with the same name cannot be confused.
  • Staged-only test: git diff-index --quiet --cached HEAD -- answers "is anything staged?".
  • Rename detection is off by default as with all plumbing; add -M if needed.
  • Prefer git status --porcelain when you also need untracked files, which diff-index does not report.