How to List Git Tags
Tags are a way of marking objects to reference them in the future. This provides us additional information about a distinct point in the history of our project. They are commonly used by developers to mark release versions of a project.
The Git Tag command is used to manage tags and can be used to create, modify and delete tags. Let's learn how to list or view the tags that are present in our repository.
We use the Git Tag command to view the tags that are present in our repository. Let's learn the different ways in which we can use this command to list the tags.
Listing all Tags
We can simply use the Git Tag command without any flags or options to view all the tags.
$ git tag
The output of this command is shown below.
Using Glob Pattern
We can use Glob Patterns with the Git Tag command to check if our repository contains tags with some specific pattern in their name. For example, if we have to check all the version 1 tags, we can use a glob pattern like "v1*". This will return the name of all the tags starting with v1.
Use the -l flag with the Git Tag command if you wish to use such patterns.
$ git tag -l "glob-pattern"
Viewing the Tag Message
In Git, we have Annotated and Lightweight(Unannotated) tags. Annotated Tags have a message associated with them. This message tells us what the tag is about. We can view this message by using the -n flag with the Git Tag. For unannotated tags, this will output the message of the commit that the tag is pointing to.
$ git tag -n
Consider the following example to understand the use of the -n flag with the Git Tag command.
- In the following image, we can see that a commit has two lines of message and also has a tag(v1.0.1). This tag is unannotated.
- When we run the Git Tag command with the -n flag, we can see that for the annotated tag the tag message is displayed. But for the unannotated tag, the commit message is displayed.
- By default, the above command will only fetch the first line of the message. We can specify the number of lines we want with the -n flag.
$ git tag -n2
- The above command will return the first two lines of the message for each tag.
We can also view the tags ordered by a certain parameter. Git provides us with the --sort option and several keys which can be used to sort tags.
We can sort tag names lexicographically(alphabetic order) by using refname as the key for the sort option.
$ git tag --sort=refname
To sort in reverse lexicographical order, use a -(hyphen) in the above command.
$ git tag --sort=-refname
We can also sort the tags according to their versions. As discussed above, we will mostly use tags to mark release versions of our project. Use the v:refname with --sort option to sort them based on their version number.
To get the tag names in reverse order of the versions, i.e. newest version first, use the -v:refname with the --sort option.
$ git tag --sort=v:refname
We can sort the tags by the date of the commit to which they point. Use the committerdate as the key for the --sort option. Use the hyphen if you want to sort in the reverse order.
$ git tag --sort=committerdate
Similarly, we can sort the tags according to the date when they were created by using the taggerdate as the key for the --sort option. Use the hyphen if you want to sort in the reverse order.
$ git tag --sort=taggerdate
Listing Remote Tags
A lot of times, we will be collaborating on a project which will be hosted on a central remote repository. To check if some other developer has added a new tag in the remote repository, we can use the Git Ls-Remote command with the --tags option. We also need to specify the remote name for the repository.
$ git ls-remote --tags <remote>
Tags are a very useful tool that can be used to mark certain points in the history of commits. We can list tags by using the Git Tag command. We can also list tags by using Glob Patterns and can view the message associated with the tags by using the -n flag. Tag names can also be viewed in various sorted order by using the --sort option with the appropriate key. The Git ls-remote command is used to view the tags of the remote repository.