我可以通过运行来达到我想要的效果:
git log -1 --decorate --oneline <commit hash>
并读取标签名称和分支名称。
有没有办法直接获取这些信息?
本质上我想要的是以下各项的反向操作:
git rev-parse <tag or branch>^{commit}
Git 2.7.0 为各种子命令引入了--points-at
标志:
git for-each-ref --points-at <commit> --format '%(refname)'
git branch --points-at <commit>
git tag --points-at <commit>
git branch --contains <commit>
git tag --contains <commit>
编辑:要找到完全指向哈希abc123
的引用,请执行以下操作:
git show-ref | awk '/^abc123/ {print $2}'
如果你没有 git 2.7+,你仍然可以使用 git for-each-ref
来查找指向特定提交的引用,方法是过滤for-each-ref
输出:
git for-each-ref | grep ^1234567
例如。 [编辑:这与使用git show-ref
基本相同,只是跟随标签到他们的目标需要更多的键入。 但是,for-each-ref 输出适用于脚本,因此如果您想编写脚本,使用它可能比使用 git show-ref
更好。