如何获取远程存储库中的提交哈希的git标签? [英] How to get git tag for a commit hash in a remote repository?
问题描述
您可以通过执行以下操作获得指向本地存储库中特定提交的标记:
You can get the tag which points at a particular commit in your local repository by doing this:
git tag --points-at <commit-hash>
或者这个:
git describe --exact-match <commit-hash>
是否甚至可以在不克隆存储库的情况下使用远程存储库呢?
Is this possible for a remote repository too, without even cloning the repository?
推荐答案
git ls-remote -t <remote> | grep <commit-hash>
git ls-remote
列出了远程存储库中的所有ref及其sha1值. -t
仅限于标签.如果您位于本地git存储库中,并且远程目录为origin
,则可以省略<remote>
.如果指定<remote>
(如https://github.com/foo/bar.git
),则可以在任何地方运行该命令.如果命令中存在<remote>
,则-t
必须在<remote>
之前.详情请参见 git-ls-remote .
git ls-remote
lists all the refs and their sha1 values in the remote repository. -t
limits to tags only. If you are under a local git repository, and the remote is origin
, <remote>
can be omitted. You can run the command anywhere if you specify <remote>
like https://github.com/foo/bar.git
. -t
must come before <remote>
if <remote>
exists in the command. See more at git-ls-remote.
更新:
否,对于git ls-remote
,没有类似--points-at
的内容.如果您知道标签名称,则git ls-remote <remote> <tag_name>
返回sha和标签,但是不可能从sha返回标签名称.
No, there is not something like --points-at
for git ls-remote
. If you know a tag name, git ls-remote <remote> <tag_name>
returns the sha and the tag, but not possible from a sha to a tag name.
有两种类型的标签.一个是轻量级标签,另一个是带注释的标签.该former只是一个ref,后者是一个git对象. Git有四种对象,commit
,tag
,tree
和blob
.
There are two types of tags. One is a lightweight tag, and the other is an annotated tag. The formmer is only a ref and the latter is a git object. Git has four kinds of objects, commit
, tag
, tree
and blob
.
如果v1.0
是轻量标签,则v1.0
和v1.0^{}
相同.
If v1.0
is a lightweight tag, v1.0
and v1.0^{}
are the same.
如果v1.0
是带注释的标签,则v1.0
是标签对象,而v1.0^{}
是它引用的提交.作为带注释的标签,v1.0
和v1.0^{}
对于许多git命令来说都是相同的,例如git log
,git show
,git diff
,当它们解析为commit
或tree
时.在git log v1.0
中,v1.0
是commit-ish
.作为commit-ish
,v1.0
和v1.0^{}
引用同一提交.因此,我们从git log v1.0
和git log v1.0^{}
获得相同的输出.对于其他git命令(如git rev-parse
),它们是不同的.在git rev-parse -t v1.0
中,v1.0
是标记对象.在git rev-parse -t v1.0^{}
中,v1.0^{}
是提交对象.
If v1.0
is an anotated tag, v1.0
is a tag object and v1.0^{}
is the commit it refers to. As an anotated tag, v1.0
and v1.0^{}
are the same for many git commands, for example git log
, git show
, git diff
, when they are resolved as commit
or tree
. In git log v1.0
, v1.0
is a commit-ish
. As a commit-ish
, v1.0
and v1.0^{}
refer to the same commit. So we get the same output from git log v1.0
and git log v1.0^{}
. For other git commands like git rev-parse
, they are different. In git rev-parse -t v1.0
, v1.0
is a tag object. And in git rev-parse -t v1.0^{}
, v1.0^{}
is a commit object.
这篇关于如何获取远程存储库中的提交哈希的git标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!