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:
a
b
c
Should I write a script to get each tag's datetime and compare them?
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:
a
b
c
Should I write a script to get each tag's datetime and compare them?
You can execute: git describe --tags $(git rev-list --tags --max-count=1) talked here: How to get latest tag name?
I'm not sure why there are no answers to what the question is asking for. i.e. All tags (non-annotated included) and without the suffix:
git describe --tags --abbrev=0
The problem with describe in CI/CD processes is you can run into the fatal: no tags can describe error.
This will occur because, per git describe --help:
The command finds the most recent tag that is reachable from a commit.
If you want the latest tag in the repo, regardless if the branch you are on can reach the tag, typically because it is not part of the current branch's tree, this command will give you the most recently created tag in the entire repo:
git tag -l --sort=-creatordate | head -n 1
If you want to find the last tag that was applied on a specific branch you can try the following:
git describe --tag $(git rev-parse --verify refs/remotes/origin/"branch_name")
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.
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.
if your tags are sortable:
git tag --merged $YOUR_BRANCH_NAME | grep "prefix/" | sort | tail -n 1
Not much mention of unannotated tags vs annotated ones here. 'describe' works on annotated tags and ignores unannotated ones.
This is ugly but does the job requested and it will not find any tags on other branches (and not on the one specified in the command: master in the example below)
The filtering should prob be optimized (consolidated), but again, this seems to the the job.
git log --decorate --tags master |grep '^commit'|grep 'tag:.*)$'|awk '{print $NF}'|sed 's/)$//'|head -n 1
Critiques welcome as I am going now to put this to use :)