Get git current branch/tag name

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:

  • If the current checkout is the HEAD of a branch, I want the branch name
  • If it is a detached HEAD, I want the tag name (on the assumption there is a tag)

  • I think you want this:

    git symbolic-ref -q --short HEAD || git describe --tags --exact-match
    

    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.

    链接地址: http://www.djcxy.com/p/19500.html

    上一篇: 如何通过pygit2获取当前签出的Git分支名称?

    下一篇: 获取git当前分支/标签名称