我在存储库中有一堆没有注释的标记,我想弄清楚它们指向哪个提交。是否有一个命令只列出标签和它们的提交sha ?检查标签和查看头部似乎有点太费力对我来说。

更新

在检查完响应后,我意识到我实际上想要的只是查看标记之前的历史记录,对于这一点git log <tagname>就足够了。

标记为answer的答案对于获取标记及其提交的列表很有用,这就是我所要求的。通过一点shell hack,我相信可以将这些转换为SHA+Commit消息。


当前回答

对于带注释的标记,git show-ref TAG显示标记的哈希值,而不是它所指向的提交的哈希值。

git show-ref——derefence TAG显示,另外,使用添加的^{}指向提交。

其他回答

一种方法是使用git rev-list。下面将输出标签所指向的提交:

$ git rev-list -n 1 $TAG

注释标签和Unannotated标签都适用

您可以在~/中添加它作为别名。如果你经常使用Gitconfig:

[alias]
  tagcommit = rev-list -n 1

然后用:

$ git tagcommit $TAG

可能的陷阱:如果您有一个本地签出或具有相同标记名称的分支,这个解决方案可能会让您“警告:refname 'myTag'是不明确的”。在这种情况下,试着增加特异性,例如:

$ git rev-list -n 1 tags/$TAG

您还可以获得更容易理解的标签指向何处使用的图片

git log --graph |git name-rev --stdin --tags |less

然后通过/滚动到您正在寻找的标记。

更紧凑的视图(——pretty=oneline)加上所有头像(-a)也可以帮助:

git log -a --pretty=oneline --graph |git name-rev --stdin --tags |less

看起来有点可怕,但也可以在~/中别名。Gitconfig(如果需要的话)。

~/.gitconfig

[alias]
ls-tags = !git log -a --pretty=oneline --graph |git name-rev --stdin --tags |less

来自Igor Zevaka:

总结

由于大约有4个几乎同样可接受但不同的答案,我将总结所有不同的方法来皮肤标签。

git rev-list -1 $TAG (answer). git rev-list outputs the commits that lead up to the $TAG similar to git log but only showing the SHA1 of the commit. The -1 limits the output to the commit it points at. git show-ref --tags (answer) will show all tags (local and fetched from remote) and their SHA1s. git show-ref $TAG (answer) will show the tag and its path along with the SHA1. git rev-parse $TAG (answer) will show the SHA1 of an unannotated tag. git rev-parse --verify $TAG^{commit} (answer) will show a SHA1 of both annotated and unannotated tags. On Windows use git rev-parse --verify %TAG%^^^^{commit} (four hats). cat .git/refs/tags/* or cat .git/packed-refs (answer) depending on whether or not the tag is local or fetched from remote.

可以使用下面,它会给提交哈希 git show -s——format=%H <tag>^{提交}

如果需要简短的提交哈希,git show -s——format=%h <tag>^{commit}

所以我有一个发布文件夹的负载,这些文件夹可以从几个不同的回购点之一签出,可能是开发,qa或主分支,也可能是生产版本,从一个标签签出,标签可能带注释,也可能没有。我有一个脚本,将查看目标文件夹,并在窗体返回答案-。问题是不同版本的git返回不同的标签签出状态。

所以我发现git show-ref——tags最初工作,除了带注释的标签。然而,添加-d将一个单独的条目添加到标记列表中,一个用于标记,另一个用于注释(注释提交被标识为^{},我用sed去掉了它)。

所以这是我剧本的核心,对于任何想要它的人:-

REPO=`git --git-dir=${TARGET} remote show origin -n | \
         grep "Fetch URL:" | \
         sed -E "s/^.*\/(.*)$/\1/" | \
         sed "s/.git$//"`

TAG=`git --git-dir=${TARGET} show-ref -d --tags | \
         grep \`git --git-dir=${TARGET} show --quiet --format=format:%H HEAD\` | \
         cut -d\  -f2 | \
         cut -d/ -f3 | \
         sed "s/\^{}$//"`

if [ "${TAG}" == "" ] ; then 
  BRANCH=`git --git-dir=${TARGET} show-ref --heads | \
         grep \`git --git-dir=${TARGET} show --quiet --format=format:%H HEAD\` | \
         cut -d\  -f2 | \
         cut -d/ -f3`
  TAG=${BRANCH}
fi