Skip to content

diff-index Git Command Guide

The git diff-index command is used to compare the content and mode of the blobs found in a tree object with the corresponding tracked files in the working tree, or with the corresponding paths in the index. When path arguments are present, it compares only paths matching those patterns. Otherwise, all tracked files are compared.

Terminal window
git diff-index [-m] [--cached] [--merge-base] [<common-diff-options>] <tree-ish> [<path>...]
OptionDescription
-mConsider merge commits when comparing
—cachedCompare the index instead of the working tree
—merge-baseCompare with the merge-base of the given commits
-p, —patchGenerate patch (combined with other options)
-s, —no-patchSuppress diff output
—statShow diffstat
-U, —unified=Generate diffs with lines of context
—rawGenerate the diff in raw format (default)
—colorShow colored diff
ParameterDescription
The tree object to compare (e.g., HEAD, commit hash)
Optional paths to limit the comparison to specific files or directories
Terminal window
git diff-index HEAD

Shows differences between the current working tree and the HEAD commit.

Terminal window
git diff-index --cached HEAD

Shows what would be committed if you ran git commit.

Terminal window
git diff-index HEAD -- myfile.txt

Compares only the specified file with the HEAD commit.

Terminal window
git diff-index -p HEAD

Generates patch information showing context and differences.

Terminal window
git diff-index --abbrev HEAD

Shows differences with abbreviated SHA-1 hashes.

Terminal window
git diff-index --cached --no-patch HEAD

Shows raw differences in the index vs HEAD without patch details.

How do I see what files have changed in the working tree compared to HEAD?

Section titled “How do I see what files have changed in the working tree compared to HEAD?”

To see what files have changed in the working tree compared to HEAD, use:

Terminal window
git diff-index HEAD

To compare what is in your index (staged changes) to HEAD, execute:

Terminal window
git diff-index --cached HEAD

How can I use git diff-index to compare only specific files?

Section titled “How can I use git diff-index to compare only specific files?”

To compare only specific files with the tree, use the path arguments:

Terminal window
git diff-index HEAD -- file1.txt file2.txt

What is the difference between —cached and non-cached modes in git diff-index?

Section titled “What is the difference between —cached and non-cached modes in git diff-index?”

—cached mode compares the index contents with the specified tree, while the default mode compares the working tree with the index. Use —cached to see what will be in your next commit.

How do I generate patch output with git diff-index?

Section titled “How do I generate patch output with git diff-index?”

To generate patch format output showing changes, use:

Terminal window
git diff-index -p HEAD

How can I suppress the content and just show which files changed?

Section titled “How can I suppress the content and just show which files changed?”

To show only file names and status without showing the actual differences, use:

Terminal window
git diff-index --name-only HEAD

Applications of the git diff-index command

Section titled “Applications of the git diff-index command”
  1. Previewing exactly what changes will be included in the next commit
  2. Comparing working tree state against any commit or tree in history
  3. Scripting automated repository checks and validation
  4. Detecting file renames, permission changes, and exact differences
  5. Building custom Git workflows and integrations requiring low-level diff operations