How to Create Tags in the Git Repository

Azhar Bashir Khan Feb 02, 2024
  1. Create Tags in the Git Repository
  2. Create Annotated Tags in the Git Repository
How to Create Tags in the Git Repository

In this tutorial, we will discuss how to create tags in the repository in Git.

Create Tags in the Git Repository

In Git, we may want to mark certain commits or specific points in the history of the project repository. For such purposes, we can use the tagging feature provided by Git.

Usually, tags are used to mark the build or release points.

Suppose we have a release of our project code, and we want to mark it with the tag v_1.0. We can do so using the git tag command.

Syntax:

git tag <tag_name>

Thus, to create the tag v_1.0, we need to execute the following command.

$ git tag v_1.0

To list the tags in our Git repository.

$ git tag
v_1.0

The tag v_1.0 we have just created with the git tag command is lightweight, a commit checksum stored in a file. It is just a pointer to a specific commit, and no other information is stored.

To view the details of the tag, we can use the git show command as follows.

$ git show v_1.0
commit c1771a7d71340aa0b6aae46598041c4390026b8d (HEAD -> master, tag: v_1.0, origin/master, origin/HEAD)
Author: John Doe <johndoe@xyz.com>
Date:   Sat Feb 26 14:12:06 2022 +0530

    modified readme

We can see above the commit associated with the tag v_1.0.

Create Annotated Tags in the Git Repository

We can also create annotated tags stored with detailed information like tagger name, email, and date in the Git database. We can also add tagging messages.

These tags can also be signed and verified with GNU Privacy Guard (GPG).

To create an annotated tag with the name v_2.0, we need to use the git tag command along with the -a option.

$ git tag -a v_2.0 -m "version 2.0"

Thus, we have now created tag v_2.0. We provided a tag message using the -m option to the git tag command.

Now, to view the details of the annotated tag, we execute the command as follows.

$ git show v_2.0
tag v_2.0
Tagger: John Doe <johndoe@xyz.com>
Date:   Sat Feb 26 14:23:05 2022 +0530

version 2.0

commit c1771a7d71340aa0b6aae46598041c4390026b8d (HEAD -> master, tag: v_2.0, tag: v_1.0, origin/master, origin/HEAD)
Author: John Doe <johndoe@xyz.com>
Date:   Sat Feb 26 14:12:06 2022 +0530

    updated readme

Thus, we see the name and email of the tagger. We can also see the tag message and, of course, the commit associated with the tag.

The tags are not pushed to the remote repository by default when executing the git push command. So, to push the tag v_2.0 to the remote repository, we need to run the git push command.

$ git push origin v_2.0

When we have many tags already present, we can push all those tags at once using the --tags option along with the git push command.

$ git push origin --tags

To learn more information about Git tags, please visit the following:

  1. Git Basics - Tagging
  2. git-tag
  3. Git tag

Related Article - Git Tag