git-diff-tree - Linux


Overview

git-diff-tree compares the content and mode of a tree object, usually a commit, to the content of a second tree object or a working directory. It primarily serves as a low-level tool for comparing snapshots of a Git repository. By default, it outputs a diff in the standard "unified" format.

Syntax

git diff-tree [-r] [-m] [--cc] [--find-copies-harder]
              [-C <n>] [-U<n>] [-p] [-s]
              [-w] [-a] [-B] [-z] [-l] [-O<order>]
              [-f] [-b] [-M] [-S] [-G] [-i] [-Z]
              [-B<blob-size>] [-I<empty-dir>]
              [--binary] [--ignore-submodules=all|dirty|untracked]
              [--combined-all] [--find-copies-harder]
              [--find-renames] [--find-renames=<mode>]
              [--ignore-name-changes] [--ignore-submodules=<mode>]
              [--ignore-unreachable] [--name-only]
              [--no-commit-id] [--no-index] [--no-literal-pathspecs]
              [--no-renames] [--no-textconv] [--order=<order>]
              [--pathspec-from-file=<file>] [--stdin]
              [--textconv=<conv>] [--word-diff=color|porcelain]
              [<tree-ish> <tree-ish>] [--] [<pathspec>...]

Options/Flags

  • -r, --recursive: Recursively compare directories.
  • -m, --merges: Skip comparing merge commits.
  • --cc: Apply change cache.
  • --find-copies-harder: Use stricter filename heuristics to detect renames.
  • -C <n>, --context <n>: Show n lines of context.
  • -U<n>, --unified=<n>: Show n lines of unified diff.
  • -p, --patch: Show a patch.
  • -s, --shortstat: Show only the number of added and deleted lines.
  • -w, --word-diff: Use word diff.
  • -a, --all: Show differences in untracked files.
  • -B, --break-rewrites: Treat both sides as rewrites.
  • -z, --null: Terminate entries with NUL.
  • -l, --line-number: Show line numbers.
  • -O<order>, --output=<order>: Output in specified order.
  • -f, --full-index: Show the full path of each file.
  • -b, --break-rewrites: Treat both sides as rewrites.
  • -M, --find-renames=<mode>: Detect renames.
  • -S, --find-copies-harder: Use stricter filename heuristics to detect renames.
  • -G, --find-renames=<mode>: Detect renames.
  • -i, --ignore-submodules=<mode>: Ignore submodules.
  • -Z, --literal-pathspecs: Interpret paths literally.
  • --binary: Treat all files as binary.
  • --ignore-submodules=<mode>: Ignore submodules.
  • --combined-all: Display common ancestor.
  • --find-copies-harder: Use stricter filename heuristics to detect renames.
  • --find-renames=<mode>: Detect renames.
  • --ignore-name-changes: Ignore changes in file names.
  • --ignore-submodules=<mode>: Ignore submodules.
  • --ignore-unreachable: Ignore unreachable commits.
  • --name-only: Show only the names of changed files.
  • --no-commit-id: Omit commit IDs from output.
  • --no-index: Compare against the working directory.
  • --no-literal-pathspecs: Interpret paths literally.
  • --no-renames: Disable rename detection.
  • --no-textconv: Disable text conversion.
  • --order=<order>: Output in specified order.
  • --pathspec-from-file=<file>: Read pathspec from a file.
  • --stdin: Read paths from stdin.
  • --textconv=<conv>: Specify text conversion program.
  • --word-diff=<color|porcelain>: Show word diff.

Examples

Compare two commits:

git diff-tree HEAD^ HEAD

Compare a commit to the working directory:

git diff-tree HEAD

Show diff in unified format:

git diff-tree --unified=3 HEAD HEAD^

Show a patch:

git diff-tree --patch HEAD HEAD^

Find renames:

git diff-tree --find-renames HEAD HEAD^

Common Issues

  • Incorrect diff results: Ensure the correct tree objects or paths are specified.
  • Failed comparison: Verify that the specified tree objects exist and are accessible.
  • Missing files: Use the -a flag to include untracked files.

Integration

git-diff-tree can be combined with other commands to perform complex operations. For instance, to create a patch of changes between two commits, use:

git diff-tree --patch HEAD HEAD^ > mypatch.patch

Related Commands

  • git-diff: Compares files in the working directory or index.
  • git-diff-index: Compares a tree object to the index.