Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the tag associated with a given git commit?

Tags:

git

For releases I usually tag with something like v1.1.0. During my build script I am creating a fwVersion.c file that contains the current git info. Currently, I have commit, and branch info in the file, but I would like to add the tag.

Is this possible?

like image 912
wes Avatar asked Sep 24 '09 20:09

wes


People also ask

How do I see tags in git?

Listing the available tags in Git is straightforward. Just type git tag (with optional -l or --list ). You can also search for tags that match a particular pattern. The command finds the most recent tag that is reachable from a commit.

What is tag in git commit?

Tags are ref's that point to specific points in Git history. Tagging is generally used to capture a point in history that is used for a marked version release (i.e. v1. 0.1). A tag is like a branch that doesn't change. Unlike branches, tags, after being created, have no further history of commits.

How can I see my repository tags?

View tags for a repository (console)In Repositories, choose the name of the repository where you want to view tags. In the navigation pane, choose Settings. Choose Repository tags.

Can you see who created a tag in git?

They're just refs in the refs/tags namespace, but they point to commits instead of tag objects. This means there are no details to show.


2 Answers

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.

If you only want to know if the commit is pointed to by a tag then you can check the output of:

git describe --exact-match <commit-id> 
like image 151
CB Bailey Avatar answered Oct 16 '22 02:10

CB Bailey


If what you want is the first tag containing the commit then:

git describe --contains <commit> 

gives the best answer IMO. If you have frequent tags than using "git tag --contains" on an old commit in a big repository can take a while to run and gives you all of the tags that contain that commit.

This form of git describe runs very quickly and gives you a single output value which is the first tag containing the commit and how far back your commit is.

like image 28
Jay Avatar answered Oct 16 '22 03:10

Jay