35

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:

  1. If the current checkout is the HEAD of a branch, I want the branch name
  2. If it is a detached HEAD, I want the tag name (on the assumption there is a tag)
Zoe
  • 27,060
  • 21
  • 118
  • 148
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
  • possible duplicate of [Get git tag of checked out revision?](http://stackoverflow.com/questions/15703253/get-git-tag-of-checked-out-revision) – Felix Kling Sep 06 '13 at 14:04
  • @FelixKling, the issue is really about getting a unified result. If you happen to check out a branch that has a tag the other method just gives the tag. So the question includes how to determine if indeed it is a detached HEAD/tag revision. – edA-qa mort-ora-y Sep 06 '13 at 14:09
  • 1
    what about this? http://stackoverflow.com/questions/1404796/how-to-get-the-latest-tag-name-in-current-branch-in-git – smcg Sep 06 '13 at 14:11

3 Answers3

77

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.

John Szakmeister
  • 44,691
  • 9
  • 89
  • 79
24

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
Xiaohui Zhang
  • 985
  • 8
  • 12
  • 1
    If a branch points to a tag and you do `git checkout branch` this command will return the tag. I think the correct order is `branch` -> `tag` as in the answer from @John Szakmeister which works fine in both cases (checking out branches and tags). – thisismydesign Nov 26 '19 at 13:30
7

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.

user187557
  • 843
  • 7
  • 6
  • I'm trying to work out under what circumstances this would return the tag name. I mean isnt every tag simply a tag on some branch (be it master or any other branch). This git command will always return the branch first, so why would it ever return the tag? – Dewald Swanepoel Nov 19 '20 at 09:53
  • 1
    Branches refer to commits, and tags refer to commits. If a branch and a tag both refer to the currently-checked-out commit, then this will return the branch. If the currently-checked out commit has a tag, but no branch, this will return the tag. If the currently-checked-out commit has no branch or tag, then this will return the commit ID. – user187557 Nov 20 '20 at 17:49