Get git current branch/tag name

后端 未结 3 1149
梦如初夏
梦如初夏 2020-12-15 05:10

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 t

相关标签:
3条回答
  • 2020-12-15 05:37

    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.

    0 讨论(0)
  • 2020-12-15 05:53

    This command can print name in this priority: tag > branch > commit

    git describe --tags --exact-match 2> /dev/null \
      || git symbolic-ref -q --short HEAD \
      || git rev-parse --short HEAD
    
    0 讨论(0)
  • 2020-12-15 05:56

    This command can print name in this priority: branch > tag > commit id

    git symbolic-ref --short -q HEAD \
      || git describe --tags --exact-match 2> /dev/null \
      || git rev-parse --short HEAD
    

    Merged @xiaohui-zhang's answer and @thisismydesign's comment. I keep coming back to this question every few months, and this is the answer I end up with, so I thought I'd post it.

    0 讨论(0)
提交回复
热议问题