Git

How to List Git Tags

The history of each git commit task can be stored by using a git tag that helps the git user to move the particular commit point to modify any file content, add or remove any file, find out any error, etc. The git user needs the list of git tags of the repository to find out the required commit point. There are many ways to find out the list of the git tags of the local and remote repository. The git command can be used to find out the list of git tags in different ways explained in this tutorial.

Prerequisites:

Install GitHub Desktop

GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the latest installer of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

Create a GitHub account

You will require to create a GitHub account to check the output of the commands in the remote server.

Create a local and remote repository

You have to create a local repository and publish the repository in the remote server to test the commands used in this tutorial. Go to the local repository folder to check the commands used in this tutorial.

Find out the existing list of git tags:

A local repository named image-gallery has been used in this tutorial to check the commands used for getting the list of git tags. Run the following command to get the list of existing git tags.

$ git tags

The output shows that the repository contains three git tags. These are V-1.0, V-2.0, and V-3.0.

Run the following command to display the git tags with the tag message.

$ git tag -n

The output shows the tag list with three tag messages those have been created before.

You can retrieve the tag information with the commit SHA value and get the tag list based on the particular commit SHA value. The `git log –online` command provides the commit history with the commit SHA values, tag information, and the commit messages. When the `git tag` command is used with the –cintains option and a particular commit SHA value, all tag lists from the commit SHA value will be printed. Run the following commands to get the tag list based on the commit SHA value.

$ git log --oneline

$ git tag --contains 792e311

The output of the first command shows the four commit SHA value from the initial commit to the last commit. The third commit SHA value in the second command has retrieved the tag list from the third commit to the last commit.

Find out the sorted list of git tags:

If the git repository contains many tags and the user wants the sorted list of git tags, the user must use the –sort option with the `git tag` command. Run the following commands to add a file in the repository, commit the task and create other tags for this commit. This new tag will require checking the function of the –sort option properly.

$ git add f4.jpg

$ git commit -m "Fourth image added"

$ git tag new-image

The following output will appear after executing the above commands.

Now, run the following command to get the sorted tag list based on the reference name.

$ git tag -l --sort=refname

The following output shows the sorted tag list in alphabetical order. The capital letter is smaller than the small letter. So, the tag name with ‘V’ has appeared first, and the tag name with ‘n’ has appeared later.

Run the following command to get the sorted tag list based on the tag version and the reference name.

$ git tag -l --sort=-version:refname

The following output will appear after executing the above command.

Find out the list of git tags based on the pattern:

When the repository contains many git tags, and the user needs to find out the particular git tags, he/she can use the pattern with the `git tag` command to retrieve the list of specific tags. Run the following command to get the list of git tags that start with the character, ‘n.’  There is only one tag in the current repository that starts with the character, ‘n.’

$ git tag -l n*

The following output will appear after executing the above command.

Find out the latest git tag:

Run the following command to find out the git tag that has been created after the last commit. Here, the –tag option has used with the `git describe` command to display the name of the latest git tag.

$ git describe --tags

The following output will appear after executing the above commands. The output shows that the new image is the latest git tag created in this tutorial before.

Find out the list of git tags from the remote server:

The git commands we used in the previous part of this tutorial are the commands for the local git repository. If the tags of the local repository were published in the remote repository, you could also retrieve the list of git tags from the remote server. Run the following command to get the list of git tags from the remote repository. You have to provide a valid git username and password to fetch the git tags.

$ git ls-remote --tags origin

The following output will appear after executing the above commands. The newly created tag, new-image didn’t publish on the remote server. So, the following output is showing the published list of git tags of the remote repository.

Conclusion:

The ways to retrieve the list of git tags from the local and remote repository have been described in this tutorial using different git commands. I hope the git users will retrieve the list of git tags based on their requirements after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.