Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically determine whether the Git checkout is a tag and if so, what is the tag name

Tags:

git

bash

shell

In a Unix or GNU scripting environment (e.g. a Linux distro, Cygwin, OSX), what is the best way to determine whether the current checkout is a Git tag. If it is a tag, how can I determine the tag name?

One use of this technique would be automatically labeling a release (like svnversion would do with Subversion).

See my related question about programmatically detecting the Git branch.

like image 219
JasonSmith Avatar asked Oct 20 '09 08:10

JasonSmith


People also ask

How do I check out a tag in git?

In order to checkout a Git tag, use the “git checkout” command and specify the tagname as well as the branch to be checked out. Note that you will have to make sure that you have the latest tag list from your remote repository.

How do you check if a commit has a tag?

Check the documentation for git describe . It finds the nearest tag to a given commit (that is a tag which points to an ancestor of the commit) and describes that commit in terms of the tag. In newer versions, git describe --tags --abbrev=0 REV will be useful when you don't want the junk on the tag.

What is my current git tag?

In order to find the latest Git tag available on your repository, you have to use the “git describe” command with the “–tags” option. This way, you will be presented with the tag that is associated with the latest commit of your current checked out branch.

Is it possible to refer to a commit by using name of a tag?

If the tag points to the commit, then only the tag is shown. Otherwise, it suffixes the tag name with the number of additional commits on top of the tagged object and the abbreviated object name of the most recent commit.


2 Answers

The solution to your question is to use

git describe --exact-match HEAD 

(which would consider only annotated tags, but you should use annotated and probably even signed tags for tagging releases).

If you want to consider all tags, also lightweight tags (which are usually used for local tagging), you can use --tags option:

git describe --exact-match --tags HEAD 

But I think you have "XY problem" here, in that you are asking question about possible solution to the problem, rather than asking question about a problem... which can have better solution.

The solution to your problem is to take a look how Git does it in GIT-VERSION-GEN script, and how it uses it in its Makefile.

like image 107
Jakub Narębski Avatar answered Oct 09 '22 03:10

Jakub Narębski


A better solution (from Greg Hewgill's answer in the other question) would be:

git name-rev --name-only --tags HEAD 

If it returns "undefined" then you are not on a tag. Otherwise it returns the tag name. So a one-liner to do something like my other answer would be:

git_tag=`git name-rev --name-only --tags HEAD | sed 's/^undefined$//'` 

Interactive shell example of how it works:

$ git checkout master Already on "master" $ git name-rev --name-only --tags HEAD undefined $ git checkout some-tag Note: moving to "some-tag" which isnt a local branch If you want to create a new branch from this checkout, you may do so (now or later) by using -b with the checkout command again. Example:   git checkout -b <new_branch_name> HEAD is now at 1234567... Some comment blah blah $ git name-rev --name-only --tags HEAD some-tag 
like image 27
JasonSmith Avatar answered Oct 09 '22 01:10

JasonSmith