By admin , 29 April 2026

Synopsis

git diff-index [--cached] [-p] <tree-ish> [<path>...]

Description

The git diff-index command is the plumbing companion to git diff. It compares a tree object (commit, tag) against the index or working tree. Output is the raw, machine-readable diff format unless -p requests a patch.

In day-to-day use, git diff-index integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git diff-index --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.

Understanding how git diff-index interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git diff-index's side effects helps avoid all three.

When to Use

Most users prefer git diff, which is easier to read. Use diff-index in scripts where you need a stable, unambiguous comparison and don't want porcelain conveniences. Hooks (especially pre-commit) often use it for precise checks.

Common Options

OptionDescription
--cachedCompare tree to index (instead of working tree).
-pShow patch.
--name-onlyList only changed file names.
--name-statusList names with status letters.
--quietExit nonzero if differences exist; produce no output.
-zNUL-terminated output.

Examples

git diff-index --cached HEAD
# Plumbing equivalent of "git diff --staged"

git diff-index HEAD
# Working tree vs HEAD

git diff-index --quiet HEAD || echo "Repo is dirty"
# Useful test in scripts and hooks

git diff-index --name-only --diff-filter=A HEAD
# List newly added files

Common Mistakes

The default raw output isn't patch text — pass -p for that. --quiet sets the exit code rather than printing, which is the opposite of what users sometimes expect from a "diff" command.

Related Commands

git diff, git diff-tree, git status, git ls-files