Development

How to List All Git Tags

In some cases, you may need to view all the tags in a git repository. In this tutorial, we will discuss how you can list all the git tags in a given git repository.
Captain Salem 3 min read
How to List All Git Tags

A Git tag is a named reference to a specific commit in a repository's history. It is commonly used to mark significant points in a project's development, such as releases, milestones, or important commits.

Tags are typically used to provide a human-readable and memorable identifier for a specific commit, making it easier to refer to that commit later. They serve as permanent markers and are not intended to change over time, unlike branches that can move as new commits are added.

Git List Local Tags

To view the tags in a local repository, we can use the git tag command with no arguments:

$ git tag

The command should return the tags available in the current repository as:

SD/688741
SD/692351
SD/693793
SD/695331
SD/700586
SD/704605
SD/706766
SD/709766
SD/709776
SD/715912
SD/717473
SD/725290
v0.1.0
v0.2.0
v0.3.0
v0.4.0
v0.5.0

Git Tag Description

To get an extensive description of the tags list, we can use -n option to the git tag command as shown:

$ git tag -n

Resulting output:

SD/688741       Move files from psl-monad submodule to super-project
SD/692351       Update files from psl-monad
SD/693793       Update files from psl-monad
SD/695331       Update files from psl-monad source-depot [SD:695331]
SD/700586       Update files from Source Depot changeset [SD:700586]
SD/704605       Update files from Source Depot changeset [SD:704605]
SD/706766       Update files from Source Depot changeset [SD:706766]
SD/709766       Bump windows-build
SD/709776       Update files from Source Depot changeset [SD:709766]
SD/715912       Update files from Source Depot changeset [SD:715912]
SD/717473       Integrate changes from SD between [SD:715912] and [SD:717473]
SD/725290       Integrate changes between [SD:717473] and [SD:725290]

Git Tag Pattern

In some cases, you may only want to fetch the tags that match a specific pattern. In that case, we can use the -l option and pass the desired pattern.

An example is as shown:

$ git tag -l *preview*

The command above should return the preview tags. An example output is as shown:

v6.1.0-preview.1
v6.1.0-preview.2
v6.1.0-preview.3
v6.1.0-preview.4
v6.2.0-preview.1

List and Sort Git Tags

We can also sort the tags from the tag command. We can use lexical or version sorting using the -sort parameter.

For example, to sort the tags in lexicographic order, we can use the command:

git tag -l --sort=refname *preview*

Output:

v6.1.0-preview.1
v6.1.0-preview.2
v6.1.0-preview.3
v6.1.0-preview.4
v6.2.0-preview.1
v6.2.0-preview.2
v6.2.0-preview.3

To list Git tags sorted by version numbers, we have to use the “git tag” command with the –sort=version:refname option and an additional tag pattern.

git tag -l --sort=-version:refname *preview*

Output:

v7.4.0-preview.3
v7.4.0-preview.2
v7.4.0-preview.1
v7.3.0-preview.8
v7.3.0-preview.7
v7.3.0-preview.6

To sort the tags based on the most recent ones, we can use the -sort=committerdate as shown in the example command below:

git tag --sort=committerdate -l *prewview*

Git List Remote Tags

To list remote tags, we can use the git ls-remote command followed with the --tag option as shown:

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

Output:

60b3b304f2e1042bcf773d7e2ae3530a1f5d52f0        refs/tags/SD/688741
5386c71bb23aff124810695e8c28bfd2173654fd        refs/tags/SD/692351
ef9ece38c27625ac3f7507b40a0a960753432e2c        refs/tags/SD/693793
273328d06f591296ae2548fcab38c17efc2466cb        refs/tags/SD/695331
3998ce06ba7c0d352278db553265ebadaec79e4c        refs/tags/SD/700586
d9bf6b037f7c859b0951d0f0a3f7dc52fae58cb5        refs/tags/SD/704605
b8dced8e80f255e0a609364e6ae8703f3e9d0248        refs/tags/SD/706766
7736f0d9e07dd0e10fc47d6f7f367e2803e133c7        refs/tags/SD/709766
b38773eb79908709628115c2f7b18eb0cb121164        refs/tags/SD/709776

Git Fetch Remote Tags

Once you've determine there are some remote tags available in your repository, you can fetch them by using the git fetch command as shown:

git fetch --all --tags

Once you've fetched the remote tags, you can list them locally by using the git tag command.

Conclusion

In this tutorial,we explored the various ways of listing tags in your git repository whether local or remote. We also covered how to sort and fetch remote tags to your local repository.

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.