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
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.
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
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.