Like many developers, I often need to find out what the most recent tag is in my git repository, usually, so I can make sure I'm tagging my next hotfix or release properly.
There are a number of ways to get this information based on various workflows, but the simplest by far is to simply use
git tag --list or
git tag -l in your CLI.
The problem with
git tag -l is that it lists ALL of the tags for a repository. That's not always an issue, but if you have a repo that's been around for a while, you could easily have hundreds of tags. No one wants to scroll through all that mess! So how do we make this more manageable?
Use git describe
git describe is a handy function that outputs an object with a human-readable name based on an available reference. For our purposes, given a couple of extra parameters, it tells us the most recent tag. To get the most recent tag, we use
git describe --abbrev=0 --tags. The
--abbrev=0 parameter tells
git describe to suppress long format tags and only show the closest tag. The
--tags parameter tells
git describe to query all tags, not just annotated ones.
Use an alias
We can simplify this by using an alias to store the command so we can call it easily later. (If you aren't familiar with aliases and how they work, check out this post)
Here is an alias I use for calling
git describe to get the most recent tag in git:
alias tag="git describe --abbrev=0 --tags"