1

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?

2
  • 1
    "…in order to use git ls-tree you need to pass the hash of a tree object…" Wrong, any commit or a pointer to a commit is ok. @, HEAD, master^2~, any branch or tag…
    – phd
    Commented Feb 10, 2021 at 16:11
  • Thank you @phd, I even tried with a commit hash on git, but I clearly made a mistake somehow. I completely misunderstood the "tree-ish" the doc refers too. Thank you very much. Commented Feb 10, 2021 at 16:22

1 Answer 1

4

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>.

6
  • Thank you @EdwardThomson for the dictionary reference and the explanation (yes, I am not english), but I think that saying "a commit is also like a tree since it has exactly one tree component and that tree can be identified by simply using the commit itself." is a mistake: a tree can belong to many commits. Commented Feb 10, 2021 at 17:28
  • this definition would be more correct: "Tree-ish" is a term that refers to any identifier [...] that ultimately leads to a (sub)directory tree. Commented Feb 10, 2021 at 17:29
  • That a commit has a single tree does not imply that a tree cannot be part of multiple commits. Commented Feb 10, 2021 at 20:56
  • (Or even part of other trees.) Commented Feb 10, 2021 at 20:56
  • 1
    Yes exactly, we are saying the same thing I guess. I simply did not like that "identified", but now I get what you meant. Basically since a commit has a single root tree, that -ish refers to the fact that there is no ambiguity in using a commit hash. Commented Feb 10, 2021 at 21:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.