Signup/Sign In

How to Create Git Tags

Tags are a way of marking specific points in the history of our project in Git. They are used to define a certain point in our project to make it easier to understand and refer to that point in the future. Most of the time, tags are used to mark release versions of our project like v1.0.1, v2.0, etc. Let's learn to create tags in Git.

Tags in Git

Creating Tags

There are two types of tags in Git - Annotated, and Lightweight. Lightweight tags are simple pointers to an object whereas Annotated tags are capable of storing additional information like the tagger name, the type of object it is pointing to, a tag message, etc.

Tags can be created by using the Git Tag command. To create an annotated tag, we also need to use the -a flag with the Git Tag command. Let's take a look at the different scenarios and learn how to create a new tag.

Create a Tag at the Current HEAD

We can use the Git Tag command to create a tag at the HEAD of our currently checked-out branch. Remember that HEAD points to the most recent commit of our current branch.

For Lightweight tags:

$ git tag <new-tag-name>

For Annotated tags, we also need to add a message to the tag. The following command will open your configured text editor where you can enter the message.

$ git tag -a <new-tag-name>

We can also use the -m flag to enter the message with the command itself.

$ git tag -a <new-tag-name> -m "Tag Message"

Create a Tag at a previous Commit

We will need the commit hash to add a tag to some previous commit point. Use the Git Log command to get the hash of the desired commit and then use the Git Tag command.

$ git tag <new-tag-name> <commit-hash>

Use the -a flag if you wish to create an annotated tag.

$ git tag -a <new-tag-name> <commit-hash>

Create a Tag using HEAD~N Notation

Instead of going through the commit history to find the hash of the commit, we can specify the commit relative to our current HEAD by using the HEAD~N notation.

For example, if we want to create a tag at the 4th commit before our current HEAD, we can use HEAD~4. But this method can only be used to create a tag on the commits of the branch where you are currently working.

$ git tag <new-tag-name> HEAD~N

Create a Tag at some other Branch

We can also create a new tag at some other branch. We will need the name of the branch where we want to create the tag. A thing to note here is that even though we are creating a tag at a branch but the tag will point to the tip(most recent commit) of the branch and will not change if we add new commits to that branch. It will continue to point at the same commit.

$ git tag <new-tag-name> <branch-name>

Use the -a flag if you wish to create an annotated tag.

$ git tag -a <new-tag-name> <branch-name>

Update an Existing Tag

Git will not allow us to have two tags with the same name. What we can do is update an existing tag to point to some other commit. We have to use the -f flag(stands for force) to do this. We can also use this to change an annotated tag to a lightweight tag and vice versa by including or excluding the -a flag.

$ git tag -f <existing-tag> <commit-hash>


Tags are an essential part of Git workflow and they are frequently used by developers to mark certain points in the history of the project.

Tags are of two types - Lightweight and Annotated. We can create lightweight tags by using the Git Tag command and annotated tags can be created by using the -a flag with the Git Tag command.

Tags can be created on the current HEAD, on some previous commit, or can be created at the tip of some other branch. In this tutorial, we discussed how to create tags with the help of the Git Tag command.

About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight