Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Inspecting History

git diff-tree command

Reference for git diff-tree: compare the contents of two tree objects or a commit and its parents, read the raw output format, and use -r, --root and --stdin in scripts.

git diff-tree is the plumbing command that compares two tree objects and prints the differences. Its output is stable and easy to parse, which is why scripts and Git's own porcelain commands (git diff, git log --raw) are built on top of it. Given a single commit, it compares that commit with its parent; given two tree-ish arguments, it compares those trees.

Unlike git diff, it never looks at the working tree or index and does not use a pager, color, or user configuration to alter the format. It does not recurse into subdirectories unless you ask with -r, so a changed directory appears as a single line.

Terminal
git diff-tree [-r] <tree-ish> <tree-ish>
git diff-tree [-r] <commit>

Try it

Bash Reading the raw format

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 Commits as arguments and --stdin

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-tree [--stdin] [-m] [-s] [-v] [--no-commit-id] [--pretty[=<fmt>]]
              [-t] [-r] [-c | --cc] [--combined-all-paths] [--root]
              [--merge-base] [<diff-options>]
              <tree-ish> [<tree-ish>] [<path>...]
Name Description
<tree-ish> A tree, or a commit/tag that is peeled to its tree. With one argument that is a commit, it is compared with its parent(s).
-r Recurse into subtrees; show file-level entries instead of directory entries.
-t With -r, also show the tree (directory) entries themselves.
--root Show the initial commit as a creation of all its files (a root commit has no parent to compare with).
--no-commit-id Do not print the commit ID line that precedes the output for a single commit.
--stdin Read commit IDs (or pairs) from standard input and compare each with its parent.
-m Also show differences for merge commits against each parent (they are skipped by default).
-c, --cc Show a combined diff for merges.
-s Suppress diff output (useful with --pretty).
--pretty[=<format>] Print the commit message header too, as git log would.
--merge-base With two commits, compare the first's merge base with the second.
<path>... Limit to paths.
<diff-options> --name-only, --name-status, -p, --stat, -M, --abbrev and more.

Raw output format. Each changed path is one line:

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

Status letters: A added, D deleted, M modified, R<score> renamed, C<score> copied, T type changed, U unmerged. A missing side has mode 000000 and an ID of all zeros. Renames list both paths separated by a tab.

Usage notes

  • Not recursive by default. Without -r, a changed file in a subdirectory appears as a modified tree (mode 040000). Almost every script wants -r.
  • Single-commit mode prints the commit ID first, then the entries. Use --no-commit-id for clean output.
  • Root and merge commits print nothing unless you pass --root or -m/-c. This is a frequent source of "empty output" surprises.
  • Rename detection is off unless you pass -M (and -C for copies); plumbing does not apply diff.renames.
  • Exit status is 0 whether or not there are differences; use --exit-code to get 1 when something differs.
  • Parse with -z if paths may contain tabs or newlines; otherwise paths are quoted with backslashes.
  • For the index or working tree use git diff-index.