git 获取标签的提交哈希
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16818025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the commit hash for a tag
提问by Kannan Ekanath
It is related to Making git show to show information in a machine parseable formatbut I am getting tired of the fact that I now have to do a lot of parsing to get the commit hash.
它与让 git show 以机器可解析的格式显示信息有关,但我已经厌倦了我现在必须进行大量解析才能获得提交哈希的事实。
Can someone give me a command that will print the commit hash (and only the commit hash) tag for a given git tag? I was hoping
有人能给我一个命令来打印给定 git 标签的提交哈希(并且只有提交哈希)标签吗?我希望
git show mylabel --pretty=format:"%H" --quiet
Would just print me my commit # but it says
只会打印我的提交 # 但它说
tag mylabel
Tagger: user <[email protected]>
Some comment
446a52cb4aff90d0626b8232aba8c40235c16245
I was expecting one line of output with just the commit line but I now have to parse for the last line?
我期待只有提交行的一行输出,但我现在必须解析最后一行?
回答by Colin Hebert
What about git log -1 --format=format:"%H" mylabel
关于什么 git log -1 --format=format:"%H" mylabel
EDIT:
编辑:
Actually a better solution would be :
实际上更好的解决方案是:
git show-ref -s mylabel
EDIT bis: As mentioned in the comments, be careful with annotated commits (which are objects of their own). To have a more generic solution, read @michas answer.
编辑之二:如评论中所述,注意带注释的提交(它们是它们自己的对象)。要获得更通用的解决方案,请阅读 @michas 答案。
You can see the difference when you do a git show-ref -d mylabel
.
当您执行git show-ref -d mylabel
.
Resources:
资源:
回答by michas
git help rev-parse
says:
git help rev-parse
说:
<rev>^{}, e.g. v0.99.8^{}
A suffix ^ followed by an empty brace pair means the object could be a tag, and dereference the tag recursively until a non-tag object is found.
Generally you use tag^{}
to refer to that commit.
通常你tag^{}
用来指那个提交。
You have two different kind of tags:
您有两种不同类型的标签:
- lightweight tags are just pointers to an existing commit
- annotated tags are objects on there own which contain a pointer to a separate commit object
- 轻量级标签只是指向现有提交的指针
- 带注释的标签是自己的对象,其中包含指向单独提交对象的指针
Use git rev-parse tag
to get the SHA1 of the tag itself.
使用git rev-parse tag
来获取标签本身的SHA1。
Use git rev-parse tag^{}
to get the SHA1 of the underlaying commit.
使用git rev-parse tag^{}
得到垫层的SHA1提交。
For lightweight tags both are the same. For annotated tags they are not.
对于轻量级标签,两者是相同的。对于带注释的标签,它们不是。
You can also use git show-ref -d tag
, which will show you both the SHA1 of the tag and the SHA1 of the associated commit.
您还可以使用git show-ref -d tag
,它会显示标签的 SHA1 和相关提交的 SHA1。
There is also git show tag
to give you details about an (annotated) tag.
还可git show tag
以为您提供有关(带注释的)标签的详细信息。
回答by Nevik Rehnel
git rev-parse mylabel^{}
should do what you want. See man gitrevisions
for more info on ^{}
and other operators.
git rev-parse mylabel^{}
应该做你想做的。有关man gitrevisions
更多信息,请参阅^{}
和其他运营商。
回答by Andy
git log <tag or branch> -1 --pretty=%H
-1
: tells it to only print 1 commit
-1
: 告诉它只打印 1 个提交
--pretty=%H
: tells it to only print the full hash
--pretty=%H
: 告诉它只打印完整的哈希
回答by Markus Mikkolainen
just a guess: try "git show --pretty=format:"%H" --quiet mylabel"
只是一个猜测:尝试“git show --pretty=format:”%H” --quiet mylabel”