Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  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)
like image 798
edA-qa mort-ora-y Avatar asked Sep 06 '13 13:09

edA-qa mort-ora-y


People also ask

How do I know what branch my tag is?

So turns out, git log is the core command for the job: With a Tag you mark a reference. So when you are on a dev branch and Tag this state. Your tag is on the actual reference. So in this case you can look to gitk or another tool where the tree is shown.

How do you fetch a tag?

To fetch tags from your remote repository, use “git fetch” with the “–all” and the “–tags” options. Let's say for example that you have a tag named “v1. 0” that you want to check out in a branch named “release”. Using this command, you have successfully checked out the “v1.

How do I get latest tag version?

JB. JB. Returns the latest tag in the current branch. To get the latest annotated tag which targets only the current commit in the current branch, use git describe --exact-match --abbrev=0 .


1 Answers

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.

like image 172
John Szakmeister Avatar answered Sep 22 '22 10:09

John Szakmeister