I'd like to attach a note to a tree object. However, in order to do so I first need to know the tree object's hash. For a given directory name that's part of my repository, how do I get its belonging tree object's hash in order to attach a note to it?
From reading this answer I understand I could use
git cat-file -p master^{tree}
to list the root tree's contents, but I'd still have to grep the output for the directory name, and follow nested tree objects recursively to get the tree object's hash for a directory deeper in the hierarchy.
Basically, I'm looking for the implementation of a fictional get-tree-hash.sh
script. If called like
get-tree-hash.sh path/to/directory/in/my/git/repo
it should output
The hash for the "repo" tree inside "path/to/directory/in/my/git" is:
92a68a2f5560fa7080393b633e2afd1d5271deef
You can do:
git rev-parse HEAD:path/to/directory/in/my/git
To print only the hash. So you don't need cut
or awk
to extract it.
Just figured it out myself,
git ls-tree HEAD -- path/to/directory/in/my/git | cut -d' ' -f3 | cut -f1
does what I want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With