Git

How to Use a Tag in Git

The developers use the git repository to keep the version control of their code, and the tags are used to store the reference points of the development phase. The developer uses a tag for a particular commit id when a new version of the code is released. The main purpose of the tag is to keep the history of the code, which can be used to visit the previous code when requires. A tag is not linked to a branch but linked to a particular commit. So, a tag name is added at the time git commit, and it is better to define a meaningful tag name to properly identify the version of the code. Different uses of tag in the git repository have shown in this tutorial.

Git tag options:

Option Purpose
-a, –annotate It is used to create an unsigned annotated tag object.
-s, –sign It is used to create a GPG-signed tag that uses the default e-mail address’s key.
–no-sign It is used to override the tag and force every tag to be signed.
-u <keyid> It is used to create a GPG-signed tag with the given key.
-f, –force It is used to replace an existing tag forcefully with the given name.
-d, –delete It is used to delete an existing tag with the given name.
 -v, –verify It is used to verify the GPG signature of the given tag name.
–sort=<key>  It is used to sort based on the key given.
 -i, –ignore-case It is used to sort and filter tags in a case insensitive manner.
-m <msg>, –message=<msg> It is used to use the given tag message instead of prompting.
 -F <file>, –file=<file> It is used to set the tag message from the given file.
–cleanup=<mode> It is used to clean up the tag message. The <mode> can be verbatim, whitespace and strip. The strip mode is the default. The verbatim mode is used to keep the message unchanged. The whitespace mode removes the leading or trailing whitespace lines. The strip mode is used to remove both whitespace and comment.
<tagname> It defines the tag name.
–help It is used to get detailed information on all tag options.

Prerequisites

1. 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.

2. Create a GitHub account
You will require to create a GitHub account to check the commands used in this tutorial.

3. Create a local and remote repository
You have to use a local repository with multiple branches that are published in the remote server to check the commands used in this tutorial.

Create Git tag

Open the local repository named upload-file from the terminal. Run the following commands to check the branch list, create a tag named single_upload and display the created tag information.

$ git branch
$ git tag single_upload
$ git show single_upload

The following output shows that the repository contains two branches, and the main is the active branch now. After creating the tag, the commit information has displayed in the output with other information.

The tag is added to the local repository, and the remote repository can be updated with this change by using the `git push` command. Run the following command to update the repository with the tag created in the local repository. You have to provide the username and password of the GitHub account after executing the `git push` command.

$ git push origin single_upload

The following output will appear if the push command is executed properly.

You can check the remote repository from github.com to confirm that the tag is added to the repository or not. The following image shows that the single_upload tag has been added to the remote repository.

Run the following commands to create another tag with the tag message and display the added tag information.

$ git tag -a single_upload-V2.0 -m 'Uploading single file'
$ git show single_upload-V2.0

The following output will appear after executing the above commands.

Run the following command to create a tag named multiple-upload-V1.0 and create a new branch named multiple by using the newly created tag.

$ git tag multiple-upload-V1.0
$ git checkout -b multiple multiple-upload-V1.0

The following output will appear if the tag and branch are created properly.

Checkout Git tag

Run the following command to checkout the tag.

$ git checkout single_upload-V2.0

The following output will appear after executing the above command.

Check tag lists

Run the following command to display the tag list of the repository.

$ git tag

The following output will appear after executing the above command.

Run the following command to display the tag list starting with ‘s‘. Two tags have created in the previous part of this tutorial starting with ‘s’.

$ git tag -l "s*"

The following output will appear after executing the above command.

Conclusion

Tag is mainly used to keep a record of the particular commit. Different ways to add the tag in the local repository have been explained in this tutorial. The tag can be added with a commit message or without a commit message. Adding a message with the tag helps the users to understand the purpose of the tag. A branch can also be created with a tag. The ways to create a simple tag, a tag with a commit message, and a branch with a tag have been described in this tutorial by using a demo local repository. I hope the concept of using the tag in the git repository will be cleared 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.