如何从 Git 提交哈希中获取所有符号名称?

jam*_*lin 8 git git-commit git-branch git-hash

如果 Git 提交哈希有多个与之关联的标签和/或多个分支的头部,是否有一种列出所有这些标签的好方法?

我已经通过选项看了git name-revgit describegit symbolic-ref,但还没有发现,似乎做我想做的任何选项。git name-rev令人沮丧的是,可以--tags选择列出标签,但没有明显的机制来只列出分支(而且git name-rev对我来说似乎总是喜欢标签而不是分支)。

$ git checkout -b branch1
$ git checkout -b branch2
$ git tag tag1
$ git tag tag2
$ git name-rev HEAD
HEAD tags/tag1
$ git describe --all HEAD
HEAD tags/tag1
$ git symbolic-ref HEAD
refs/heads/branch2
Run Code Online (Sandbox Code Playgroud)

要映射提交哈希所有的符号名,我将需要运行git tag --listgit branch --all --list,然后运行git rev-parse在所有的结果?

Eri*_*kMD 7

由于以下git for-each-ref命令,应该可以实现您想要的:

git for-each-ref --points-at=HEAD
Run Code Online (Sandbox Code Playgroud)

完成示例会话:

$ git init
$ touch a
$ git add a
$ git commit -m a
[master (root-commit) eb3222d] a
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a
$ git checkout -b branch1
Switched to a new branch 'branch1'
$ git checkout -b branch2
Switched to a new branch 'branch2'
$ git tag tag1
$ git tag tag2
$ git tag -a tag3 -m "annotated tag"
$ git for-each-ref --points-at=HEAD
eb3222db1821633a54ccd0a6434e883c4cb71b98 commit refs/heads/branch1
eb3222db1821633a54ccd0a6434e883c4cb71b98 commit refs/heads/branch2
eb3222db1821633a54ccd0a6434e883c4cb71b98 commit refs/heads/master
eb3222db1821633a54ccd0a6434e883c4cb71b98 commit refs/tags/tag1
eb3222db1821633a54ccd0a6434e883c4cb71b98 commit refs/tags/tag2
0dbba96f519c2ad1b470f97171230004addff896 tag    refs/tags/tag3
Run Code Online (Sandbox Code Playgroud)