Search code examples
linuxgitinternals

How to git ls-tree from a commit object?


From documentation, in order to use git ls-tree you need to pass the hash of a tree object. What if I want to obtain the same output of git ls-tree starting from a commit object. I can obviously do it with something like this:

git ls-tree $(git cat-file -p my_commit | grep -oP "(?<=tree ).*")

But I feel like I am reinventing the wheel. Is there a git command that already does this?


Solution

  • No, git ls-tree takes a tree-ish object.

    The "-ish" suffix here is important. Per the Cambridge Dictionary:

    -ish suffix (QUITE)

    used to form adjectives to give the meaning to some degree; fairly:

    • He had a sort of reddish beard.
    • She was oldish - about 60, I'd say.
    • We'll start at sevenish (= about seven o'clock).

    In this case, "tree-ish" means like a tree. A tree, of course, is like a tree. But a commit is also like a tree since it has exactly one tree component; that means that you can unambiguously refer to that tree by simply using the commit itself.

    So, just do git ls-tree <commit-ish>.