On Git, tags are used in order to define commits in your history that may be more important than others.
When you are performing a merge commit, right before deploying, you might want to tag this commit.
This way, if you choose to go back to the previous version, you will be able to find the commit in the blink of an eye.
However, in order to find this commit, you will need to list the existing Git tags of your repository.
In this tutorial, you will learn how you can easily list Git tags on your repository.
List Local Git Tags
In order to list Git tags, you have to use the “git tag” command with no arguments.
$ git tag v1.0 v2.0
You can also execute “git tag” with the “-n” option in order to have an extensive description of your tag list.
$ git tag -n
Optionally, you can choose to specify a tag pattern with the “-l” option followed by the tag pattern.
$ git tag -l <pattern>
Awesome, you have successfully listed tags on your Git repository!
List and Sort Git Tags
Additionally, you can choose to list your tags and sort them following a lexical or a version sorting.
In order to list Git tags following a lexicographic order, you have to use the “git tag” command with the “–sort=refname” option and an additional tag pattern.
$ git tag -l --sort=refname <pattern>
Sorting by refname is not the only way of sorting Git tags.
You can also choose to sort your tags by versions : this way, your tag names will be treated as version numbers.
In order to list Git tags sorted by version numbers, you have to use the “git tag” command with the “–sort=version:refname” option and an additional tag pattern.
$ git tag -l --sort=-version:refname <pattern>
Sorting most recents Git tags
In order to list and sort Git tags by their latest Git activity, you can use the “git tag” command with the “–sort=committerdate”.
$ git tag --sort=committerdate -l <pattern>
Congratulations, you successfully sorted your Git tags using the sort options!
List Remote Git Tags
As you already know it, Git is a decentralized versioning system.
As a consequence, you may have not fetched some tags that have been made available by other developers on your repository.
In order to list remote Git tags, you have to use the “git ls-remote” command with the “–tags” option and the name of your remote repository.
$ git ls-remote --tags <remote>
For example, if your remote name is “origin”, you will have to execute the following command.
$ git ls-remote --tags origin 53a7dcf1ca57e05d456321b406730b39dc8ed75e refs/tags/v1.0 7a9ad7fd794bf52a11de43aacc6010978e6100d3 refs/tags/v2.0
Note : as you can see, by using the “ls-remote” command, you are presented with tags using the “refs” syntax that was already explained in our previous tutorials.
Fetching Remote Tags Easily
Now that you know that some tags are available remotely, you may want to fetch your tags in order to list them locally.
To fetch tags from your remote repository, use “git fetch” with the “–all” and the “–tags” options.
$ git fetch --all --tags Fetching origin From git-repository 53a7dc..7a9ad7 master -> origin/master * [new tag] v1.0 -> v1.0 * [new tag] v1.0 -> v2.0
Awesome, you fetched your tags from your distant repository!
Now, you can list them easily using the “git tag” command (with no arguments!)
$ git tag v1.0 v2.0
Find Latest Git Tag Available
In order to find the latest Git tag available on your repository, you have to use the “git describe” command with the “–tags” option.
This way, you will be presented with the tag that is associated with the latest commit of your current checked out branch.
$ git describe <latest_tag>
In this tutorial, you learnt how you can easily list your Git tags, whether you they are local or remote.
You also learnt that you can use advanced sorting options in order to have your results sorted by recent date or version number.
We have other tutorials on Git tags, make sure to have a read :
If you are interested in Software Engineering or in Git, we have a complete section dedicated to it on the website, so make sure to check it out!