Saleh Rezq
Saleh Rezq

Reputation: 321

Why does `git diff-tree <tree-ish>` not print a result when applied to the very first commit?

I usually use git diff-tree <tree-ish> with these options: --no-commit-id --name-status -r or --no-commit-id --name-only -r to list the added and changed files within a specific commit.

I just noticed today that this command does not print a result when applied to the very first commit!.

So why, and how to overcome it?

Upvotes: 1

Views: 1438

Answers (1)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391276

The reason is likely because the documentation states:

When two trees are given, it compares the first tree with the second. When a single commit is given, it compares the commit with its parents.

The first commit in a repository does not have a parent.

However, you can specify --root for that commit:

--root
When --root is specified the initial commit will be shown as a big creation event. This is equivalent to a diff against the NULL tree.

Note that if you're building some kind of script or application that outputs these things you will have to detect that it is the first commit, as adding --root elsewhere will not be what you want.

So while this yields no output:

git diff-tree -r HASH-OF-FIRST-COMMIT

This should compare the first commit against an empty tree:

git diff-tree -r --root HASH-OF-FIRST-COMMIT

Upvotes: 1

Related Questions