diff-tree Git Command Guide
The git diff-tree command is used to compare the content and mode of blobs found via two tree objects. If there is only one tree reference given, the commit is compared with its parents.
git diff-tree Syntax:
Section titled “git diff-tree Syntax:”git diff-tree [--stdin] [-m] [-s] [-v] [--no-commit-id] [--pretty] [-t] [-r] [-c | --cc] [--combined-all-paths] [--root] [--merge-base] [<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]Options:
Section titled “Options:”| Option | Description |
|---|---|
| —stdin | Read commit references from stdin |
| -m | Consider merge commits |
| -s | Suppress diff output |
| -v | Verbose mode |
| —no-commit-id | Suppress commit ID output |
| —pretty | Show header information in pretty format |
| -t | Show tree entry for root |
| -r | Recurse into sub-trees |
| -c, —cc | Show combined diffs for merges |
| —combined-all-paths | Include all paths in combined diffs |
| —root | Include root when comparing with initial commit |
| —merge-base | Compare with merge-base of given commits |
| -p, —patch | Generate patch output |
| —stat | Show diff statistics |
| —raw | Generate raw diff format (default) |
| —color | Show colored diff |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| First tree/commit to compare | |
| [ | Optional second tree/commit (default: parents) |
| [ | Optional paths to limit comparison |
git diff-tree Command Samples:
Section titled “git diff-tree Command Samples:”Compare two commits with patch output
Section titled “Compare two commits with patch output”git diff-tree -p HEAD~1 HEADShows the differences between two commits in patch format.
Show diff statistics
Section titled “Show diff statistics”git diff-tree --stat HEAD~1 HEADDisplays summary statistics of changes between commits.
Compare with merge-base
Section titled “Compare with merge-base”git diff-tree --merge-base HEAD HEAD~3Compares current HEAD with the merge-base of HEAD and HEAD~3.
Read commits from stdin
Section titled “Read commits from stdin”echo "HEAD" | git diff-tree --stdin --prettyReads commit references from standard input.
Show combined diff for merge commit
Section titled “Show combined diff for merge commit”git diff-tree -c -p HEADDisplays combined diff format for a merge commit showing changes from all parents.
How do I compare two specific commits?
Section titled “How do I compare two specific commits?”To compare two specific commits, use:
git diff-tree <commit1> <commit2>How can I show diffs for merge commits?
Section titled “How can I show diffs for merge commits?”To show diffs for merge commits, use the -c or —cc option:
git diff-tree -c HEADHow do I process multiple commits in a script?
Section titled “How do I process multiple commits in a script?”To process multiple commits, use —stdin with commit IDs:
echo -e "HEAD~1\nHEAD" | git diff-tree --stdin --prettyWhat is the difference between -c and —cc?
Section titled “What is the difference between -c and —cc?”-c shows combined diff with one column per parent, while —cc omits any commit that contains only changes from one side of the merge.
How can I limit the diff to specific paths?
Section titled “How can I limit the diff to specific paths?”To limit to specific paths, add them at the end:
git diff-tree HEAD~1 HEAD -- path/to/fileHow do I show only names and status of changed files?
Section titled “How do I show only names and status of changed files?”To show only names and status, use with —name-status:
git diff-tree --name-status HEAD~1 HEADApplications of the git diff-tree command
Section titled “Applications of the git diff-tree command”- Analyzing commit changes and code evolution
- Scripting automated diff processing in CI/CD pipelines
- Building repository analysis and audit tools
- Creating custom Git workflow integrations
- Detecting file changes and renames between commits