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!