Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git 如何获取给定目录名的树哈希?_Git_Git Notes - Fatal编程技术网

Git 如何获取给定目录名的树哈希?

Git 如何获取给定目录名的树哈希?,git,git-notes,Git,Git Notes,我想在树对象上附加注释。然而,为了做到这一点,我首先需要知道树对象的散列。对于作为存储库一部分的给定目录名,如何获取其所属树对象的哈希值,以便向其附加注释 从阅读中我了解到我可以 git cat-file -p master^{tree} 列出根目录树的内容,但我仍然需要对目录名的输出进行grep处理,并递归地跟踪嵌套的树对象,以获取层次结构中更深层目录的树对象哈希 基本上,我正在寻找一个虚构的get tree hash.sh脚本的实现。如果像这样称呼 get-tree-hash.sh pat

我想在树对象上附加注释。然而,为了做到这一点,我首先需要知道树对象的散列。对于作为存储库一部分的给定目录名,如何获取其所属树对象的哈希值,以便向其附加注释

从阅读中我了解到我可以

git cat-file -p master^{tree}
列出根目录树的内容,但我仍然需要对目录名的输出进行grep处理,并递归地跟踪嵌套的树对象,以获取层次结构中更深层目录的树对象哈希

基本上,我正在寻找一个虚构的
get tree hash.sh
脚本的实现。如果像这样称呼

get-tree-hash.sh path/to/directory/in/my/git/repo
它应该输出

The hash for the "repo" tree inside "path/to/directory/in/my/git" is:
92a68a2f5560fa7080393b633e2afd1d5271deef

只是我自己想出来的

git ls-tree HEAD -- path/to/directory/in/my/git | cut -d' ' -f3 | cut -f1
做我想做的事。

你可以做:

git rev-parse HEAD:path/to/directory/in/my/git

只打印散列。因此,您不需要
cut
awk
来提取它。

正如我在回答中提到的,您也可以使用
ls-files-s
以及
-s
在这里有何帮助?我对阶段性内容不感兴趣。同意,我首先误解了你。
。|awk“{print$3}”
有点笨拙。这个答案对我来说比rev parse更有用,因为它适用于绝对路径。这确实更好。有关此语法的详细信息已记录在案。