Show which git tag you are on?

GitGit CheckoutGit Tag

Git Problem Overview


I'm having trouble finding out which tag is currently checked out.

When I do:

git checkout tag1
git branch

I can't seem to find out which tag I'm on. It only logs:

* (no branch)
master

Is it possible to find out which tags are checked out? In the above example, this would be tag1.

Git Solutions


Solution 1 - Git

Edit: Jakub Narębski has more git-fu. The following much simpler command works perfectly:

git describe --tags

(Or without the --tags if you have checked out an annotated tag. My tag is lightweight, so I need the --tags.)

original answer follows:

git describe --exact-match --tags $(git log -n1 --pretty='%h')

Someone with more git-fu may have a more elegant solution...

This leverages the fact that git-log reports the log starting from what you've checked out. %h prints the abbreviated hash. Then git describe --exact-match --tags finds the tag (lightweight or annotated) that exactly matches that commit.

The $() syntax above assumes you're using bash or similar.

Solution 2 - Git

This worked for me git describe --tags --abbrev=0

Edit 2020: As mentioned by some of the comments below, this might, or might not work for you, so be careful!

Solution 3 - Git

Show all tags on current HEAD (or commit)

git tag --points-at HEAD

Solution 4 - Git

git describe is a porcelain command, which you should avoid:

http://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html

Instead, I used:

git name-rev --tags --name-only $(git rev-parse HEAD)

Solution 5 - Git

When you check out a tag, you have what's called a "detached head". Normally, Git's HEAD commit is a pointer to the branch that you currently have checked out. However, if you check out something other than a local branch (a tag or a remote branch, for example) you have a "detached head" -- you're not really on any branch. You should not make any commits while on a detached head.

It's okay to check out a tag if you don't want to make any edits. If you're just examining the contents of files, or you want to build your project from a tag, it's okay to git checkout my_tag and work with the files, as long as you don't make any commits. If you want to start modifying files, you should create a branch based on the tag:

$ git checkout -b my_tag_branch my_tag

will create a new branch called my_tag_branch starting from my_tag. It's safe to commit changes on this branch.

Solution 6 - Git

git log --decorate

This will tell you what refs are pointing to the currently checked out commit.

Solution 7 - Git

Here's a fun one for a certain set of use cases. If your repository has versions such as v1.0.0, v1.1.0, v1.1.1 etc, and also shorthand versions such as v1 that point to whatever is the latest v1.x.x, the following will give you a reference to the currently-checked-out commit in relation to the most recent fully versioned tag, with fallbacks if that doesn't work:

git describe --tags --exact-match --match "v*.*.*" \
  || git describe --match "v*.*.*" --tags \
  || git describe --tags \
  || git rev-parse HEAD

So let's say you have the following commits:

* 4444444 (main, origin/main, tag: v2.0.0, tag: v2.0, tag: v2)
* 3333333
* 2222222 (tag: v1.1.0, tag: v1.1, tag: v1)
* 1111111 (tag: v1.0.0, tag: v1.0)
* 0000000

Output of the above command for a few example HEADs:

  • git checkout main -> v2.0.0
  • git checkout 3333333 -> v1.1.0-1-g3333333
  • git checkout 2222222 -> v1.1.0
  • git checkout v1 -> v1.1.0
  • git checkout 0000000 -> 0000000 (full ref)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongrmView Question on Stackoverflow
Solution 1 - GitbstpierreView Answer on Stackoverflow
Solution 2 - GitM KView Answer on Stackoverflow
Solution 3 - GitGeorge PavelkaView Answer on Stackoverflow
Solution 4 - GitGregView Answer on Stackoverflow
Solution 5 - GitmipadiView Answer on Stackoverflow
Solution 6 - GitchriswatrousView Answer on Stackoverflow
Solution 7 - GitsnazzyboucheView Answer on Stackoverflow