Tag is a useful feature of the git. The specific point of the repository can be identified by defining the tag for that point. It is mainly used to keep the release version of the repository, and the user will be able to move in the earlier version of the repository by using the tag. The tag can be created for a specific commit of the git history. To create this type of tag, the commit SHA will be required at the time of creating the tag. It is better to know how to create the tag for the repository before adding a tag for a specific commit. The ways to create the simple tag and a tag for the specific commit have 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 used in this tutorial.

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.

Create a simple git tag:

You can create a new local repository or any existing repository to check the commands used in this part of this tutorial. I have used an existing local repository named image-gallery and opened the repository folder from the terminal. Run the following commands to check the current status of the repository, add an untracked file, commit the task, create a tag, and display the list of the existing tag.

$ git status


$ git add f1.jpg


$ git commit -m “First image added.”


$ git tag V-1.0


$ git tag -n

The following output shows that the git repository has three untracked files, and the file f1.jpg has been added to the repository. This task has been committed. Next, a tag named V-1.0 has created and displayed.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-add-tag-specific-commit-in-git-1.png" data-lazy- height="331" src="data:image/svg xml,” width=”867″>

Create git tag for a commit:

The commit SHA value is required to add a git tag for that specific commit. The `git log` command with –oneline option is used to get the short SHA code of a commit. Run the following commands to check the current git status, add an untracked file named f2.jpg, commit the task, and get the list of all commits with a short SHA value.

$ git status


$ git add f2.jpg


$ git commit -m “Second image added.”


$ git log –oneline

The following output shows that the repository has two untracked files, one file is added, and the task is committed. Next, the `git log` command shows that the list of three commit tasks with SHA values.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-add-tag-specific-commit-in-git-2.png" data-lazy- height="328" src="data:image/svg xml,” width=”884″>

Select the commit SHA value from the log output and run the following commands to create a tag for that specific commit and check the log information again.

$ git tag V-2.0 792e311 -m “Tag is added for the second commit with V-2.0”


$ git log –oneline

The following output shows that a tag named V-2.0 has been added for the commit SHA value, 792e311.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-add-tag-specific-commit-in-git-3.png" data-lazy- height="135" src="data:image/svg xml,” width=”897″>

Create git tag for the last commit:

Creating a new tag-based on any commit SHA value has been shown in the previous example. But if you want to create a tag for the last commit, then you don’t need to know the Commit SHA value. A new file has been added, and the task has been committed like the previous part of this tutorial. Run the `git log –online` command to check the last committed task.

The following output shows that the last committed message is “Third image added,” and it has committed in the branch main.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-add-tag-specific-commit-in-git-4.png" data-lazy- height="112" src="data:image/svg xml,” width=”844″>

The last committed task can be referred by the HEAD, and you don’t need the commit SHA to create a tag for the last commit. Run the following commands to create a tag for the last commit with HEAD and display the log information after creating the tag.

$ git tag V-3.0 HEAD -m “Tag is added for the third commit”


$ git log –oneline

The following output shows that a tag named V-3.0 has been created for the last commit.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-add-tag-specific-commit-in-git-5.png" data-lazy- height="152" src="data:image/svg xml,” width=”895″>

Now, open the repository from the GitHub Desktop. The following image shows three commits’ messages with the three tag names created in the previous part of this tutorial. Now, click on the Publish repository option to publish the updated content of the local repository to the remote repository.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-add-tag-specific-commit-in-git-6.png" data-lazy- height="395" src="data:image/svg xml,” width=”918″>

When the repository is published through GitHub Desktop, then it does not publish the added tag information. So, the added tag can be published to the remote repository by using the `git push` command. Run the following command to push the tag information to the remote server. You have to provide the username and password of the git account to execute the command properly.

The following output shows that three new tag entries have been pushed into the remote repository.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-add-tag-specific-commit-in-git-7.png" data-lazy- height="236" src="data:image/svg xml,” width=”895″>

Now, login to the git account from github.com and open the remote repository named image-gallery that has been published in the earlier step. When the tag link has clicked, then the information has appeared. All the published tags with tag messages are displaying here.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-add-tag-specific-commit-in-git-8.png" data-lazy- height="564" src="data:image/svg xml,” width=”886″>

Conclusion:

Different ways to add the tag-specific commit have been described in this tutorial by using a demo git repository. A simple tag, a tag with a specific commit SHA, and a tag for the last commit have been created in this tutorial to help the readers know how to create a tag and apply it properly in their repository.

About the author

<img alt="Fahmida Yesmin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/channel-logo-150×150.jpg609fd217a85e2.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.