获取git当前分支/标签名称 [英] Get git current branch/tag name
问题描述
我如何获得我的工作副本的当前分支或标签名称?我已经看到引用指出 rev-parse --abbrev-ref HEAD
会给出分支名称,但如果结帐是标签,这种情况不起作用,在这种情况下它只是返回'HEAD'。我需要以某种方式获得这些修订标记的名称。
How can I get the current branch or tag name for my working copy? I have seen references that indicate rev-parse --abbrev-ref HEAD
will give branch name, but this doesn't work if the checkout is of a tag, in which case it just returns 'HEAD'. I need to somehow get the tag name of these revisions.
要清楚,我需要两个可能的名称之一:
To be clear, I want one of two possible names:
- 如果当前签出是分支的HEAD,我需要分支名称
- 如果它是分离的HEAD,我希望标签名称(假设有一个标签)
推荐答案
我想你想要这个: p>
I think you want this:
git symbolic-ref -q --short HEAD || git describe --tags --exact-match
这将输出HEAD的值,如果不是分离,或发出标签名称,如果它完全匹配。否则会显示错误。
That will output the value of HEAD, if it's not detached, or emit the tag name, if it's an exact match. It'll show you an error otherwise.
这篇关于获取git当前分支/标签名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!