What\'s the simplest way to get the most recent tag in Git?
git tag a HEAD
git tag b HEAD^^
git tag c HEAD^
git tag
output:
What is wrong with all suggestions (except Matthew Brett explanation, up to date of this answer post)?
Just run any command supplied by other on jQuery Git history when you at different point of history and check result with visual tagging history representation (I did that is why you see this post):
$ git log --graph --all --decorate --oneline --simplify-by-decoration
Todays many project perform releases (and so tagging) in separate branch from mainline.
There are strong reason for this. Just look to any well established JS/CSS projects. For user conventions they carry binary/minified release files in DVCS. Naturally as project maintainer you don't want to garbage your mainline diff history with useless binary blobs and perform commit of build artifacts out of mainline.
Because Git uses DAG and not linear history - it is hard to define distance metric so we can say - oh that rev is most nearest to my HEAD
!
I start my own journey in (look inside, I didn't copy fancy proof images to this long post):
What is nearest tag in the past with respect to branching in Git?
Currently I have 4 reasonable definition of distance between tag and revision with decreasing of usefulness:
HEAD
to merge base with tagHEAD
and tagI don't know how to calculate length of shortest path.
Script that sort tags according to date of merge base between HEAD
and tag:
$ git tag \
| while read t; do \
b=`git merge-base HEAD $t`; \
echo `git log -n 1 $b --format=%ai` $t; \
done | sort
It usable on most of projects.
Script that sort tags according to number of revs that reachable from HEAD but not reachable from tag:
$ git tag \
| while read t; do echo `git rev-list --count $t..HEAD` $t; done \
| sort -n
If your project history have strange dates on commits (because of rebases or another history rewriting or some moron forget to replace BIOS battery or other magics that you do on history) use above script.
For last option (date of tag regardless merge base) to get list of tags sorted by date use:
$ git log --tags --simplify-by-decoration --pretty="format:%ci %d" | sort -r
To get known current revision date use:
$ git log --max-count=1
Note that git describe --tags
have usage on its own cases but not for finding human expected nearest tag in project history.
NOTE You can use above recipes on any revision, just replace HEAD
with what you want!
For the question as asked,
How to get the latest tag name in the current branch
you want
git log --first-parent --pretty=%d | grep -m1 tag:
--first-parent
tells git log
not to detail any merged histories, --pretty=%d
says to show only the decorations i.e. local names for any commits. grep -m1
says "match just one", so you get just the most-recent tag.
if your tags are sortable:
git tag --merged $YOUR_BRANCH_NAME | grep "prefix/" | sort | tail -n 1
git tag --sort=-refname | awk 'match($0, /^[0-9]+\.[0-9]+\.[0-9]+$/)' | head -n 1
This one gets the latest tag across all branches that matches Semantic Versioning.
My first thought is you could use git rev-list HEAD
, which lists all the revs in reverse chronological order, in combination with git tag --contains
. When you find a ref where git tag --contains
produces a nonempty list, you have found the most recent tag(s).
If you need a one liner which gets the latest tag name (by tag date) on the current branch:
git for-each-ref refs/tags --sort=-taggerdate --format=%(refname:short) --count=1 --points-at=HEAD
We use this to set the version number in the setup.
Output example:
v1.0.0
Works on Windows, too.