Git most recent tag
Friday, May 3, 2019
"Most recent" could have two meanings in terms of git.
You could mean, "which tag has the creation date latest in time", and most of the answers here are for that question. In terms of your question, you would want to return tag c .
Or you could mean "which tag is the closest in development history to some named branch", usually the branch you are on, HEAD . In your question, this would return tag a .
These might be different of course:
A->B->C->D->E->F (HEAD)
\ \
\ X->Y->Z (v0.2)
P->Q (v0.1)
Imagine the developer tag'ed Z as v0.2 on Monday, and then tag'ed Q as v0.1 on Tuesday. v0.1 is the more recent, but v0.2 is closer in development history to HEAD, in the sense that the path it is on starts at a point closer to HEAD.
I think you usually want this second answer, closer in development history. You can find that out by using git log v0.2..HEAD etc for each tag. This gives you the number of commits on HEAD since the path ending at v0.2 diverged from the path followed by HEAD.
Here's a Python script that does that by iterating through all the tags running this check, and then printing out the tag with fewest commits on HEAD since the tag path diverged:
https://github.com/MacPython/terryfy/blob/master/git-closest-tag
git describe does something slightly different, in that it tracks back from (e.g.) HEAD to find the first tag that is on a path back in the history from HEAD. In git terms, git describe looks for tags that are "reachable" from HEAD. It will therefore not find tags like v0.2 that are not on the path back from HEAD, but a path that diverged from there.
From < https://stackoverflow.com/questions/1404796/how-to-get-the-latest-tag-name-in-current-branch-in-git >
@matthew-brett
List tags starting with "v":
git tag -l v* --sort=-taggerdate
Get latest tag across all branches, then count commits between there and here. The trailing two dots `..` are significant!
git for-each-ref refs/tags --sort=-taggerdate --format=%(refname) --count=2
git rev-list --count refs/tags/v5.9..
#alternate:
git rev-list --count refs/tags/v5.9..HEAD
Common ancestor according to Git Ext graph | c9113db26036680855ba7c80160f64ab2a8ad592 |
number of commits between HEAD and common ancestor | 66 |
git merge-base HEAD v5.9 | bcb9ed8047aaf726fa2ed35ad07e507f1c7a5442 |
num commits between merge-base and c9113db | 44 |
git rev-list --count bcb9ed8047aaf726fa2ed35ad07e507f1c7a5442 .. c9113db26036680855ba7c80160f64ab2a8ad592 | 309 |
Why is the rev-list count between merge-base and common ancestor 309, yet only 100 when counting by segments?
git merge-base picks "Preparing for Leo" (6) as the common ancestor, yet there are 3 newer merge commits in between (3,4). Why pick the older one? And why 2 back from the actual merge commit (5)? Why does the colour change from purple to black at the older one?
(2) is the target, the commit closest to HEAD.
Oooohh. (2) is the common ancestor on the grey branch, not purple/black!
=========================================================
# The command finds the most recent tag that is reachable from a commit.
# 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.
git describe
# With --abbrev set to 0, the command can be used to find the closest tagname without any suffix:
git describe --abbrev=0
# other examples
git describe --abbrev=0 --tags
# gets tag from current branch
git describe --tags `git rev-list --tags --max-count=1`
# gets tags across all branches, not just the current branch
rponte commented Sep 10, 2015
From < https://gist.github.com/rponte/fdc0724dd984088606b0 >