LinuxCommandLibrary

git-diff-tree

Compare content differences between tree objects

TLDR

Compare two tree objects

$ git diff-tree [tree-ish1] [tree-ish2]
copy

Show changes between two specific commits
$ git diff-tree -r [commit1] [commit2]
copy

Display changes in patch format
$ git diff-tree [[-p|--patch]] [tree-ish1] [tree-ish2]
copy

Filter changes by a specific path
$ git diff-tree [tree-ish1] [tree-ish2] -- [path/to/file_or_directory]
copy

SYNOPSIS

git diff-tree [-p] [-r] [--name-only] [--name-status] <tree-ish1> <tree-ish2>

PARAMETERS

-p
    Generate patch (default).

-r
    Recurse into subtrees.

--name-only
    Show only names of changed files.

--name-status
    Show only names and status of changed files.

<tree-ish1>
    The first tree to compare. This can be a tree SHA-1, tag, or branch name that resolves to a tree.

<tree-ish2>
    The second tree to compare. Similar to <tree-ish1>, this also resolves to a tree.

--full-index
    Show the full pre and post image object names on the "index" line rather than just the first few characters.

--root
    Treat the content of the first tree as living at / and generate diff against empty tree.

DESCRIPTION

The git-diff-tree command is a plumbing command that compares the content and mode of blobs found via two tree objects. It is used primarily by scripts and other Git commands to understand the differences between versions of a project's files. It can output a detailed patch that can be applied to another branch, show a summary of changes, or simply list the files that have been modified, added, or deleted. This command provides a low-level mechanism for analyzing differences and is essential for understanding how changes propagate through the Git repository. The output format can be tailored to specific needs, making it a flexible tool for both automated processing and human inspection.

Unlike `git diff`, this command operates directly on tree objects within the Git object database, bypassing the need for a working directory.

USAGE EXAMPLES

Example 1: Show the diff between two commits:
git diff-tree -p commit1 commit2

Example 2: Show only the names of changed files between two trees:
git diff-tree --name-only tree1 tree2

Example 3: Show changes including subdirectories:
git diff-tree -r -p tree1 tree2

OUTPUT FORMAT

The output format depends on the options used. The default output (-p) shows a standard diff patch. The `--name-only` option shows a list of file names and the `--name-status` shows a short status code along with the file name. Understanding the output requires familiarity with the Git diff format.

HISTORY

git-diff-tree has been a core part of Git since its inception, providing the underlying mechanism for comparing different states of the repository. Its development has been driven by the need for precise and scriptable analysis of changes between commits. Over time, options have been added to improve its flexibility and output formats.

SEE ALSO

git diff(1), git cat-file(1), git ls-tree(1)

Copied to clipboard