Get git current branch/tag name

后端 未结 3 1160
梦如初夏
梦如初夏 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: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.

提交回复
热议问题