Signup/Sign In

Git Tag

In our day to day to life, we use tags to classify things or to mark them for future reference. That's pretty much the same case in Git. In Git, we use tags to mark specific points in our project history. Most of the time these points are software release versions like v1.0.10, v1.2, etc. Let's learn more about tagging and the Git Tag command.

Tag attached to a commit

Tagging

  • Tagging is a way of marking important points in the history of our project so that we can reference them in the future. In Git tagging is done by using the Git Tag command.
  • Tags are mostly used to mark software release versions or to mark a specific commit that we may need in the future.
  • There are two types of tags in Git - Annotated and Unannotated(Lightweight).
  • We can also push tags to a remote repository to let others know what changes we have marked.

Types of Tags

Git provides us with two types of tags - Unannotated(Lightweight) and Annotated. Let's try to understand each one of them.

Lightweight Tags

Lightweight or Unannotated tags are just pointers to specific commit points. A lightweight tag just stores the hash of the commit it points to and no other information. They are mainly used on local systems only and it is not recommended to push them to the remote repository as they do not add much value. We can view the tags by navigating to the .git/refs/tags directory. We can see that the content of the lightweight tag file contains just the hash of the commit where we created the tag.

Viewing the content of light weighted tag

Annotated Tags

Annotated tags are tags that contain additional information and not just the hash of the commit. The additional information(called metadata) can have fields like the type of object the tag points to, the name and email of the person who created the tag, a tag message. They even have their own hash as they are objects and not just pointers. Annotated tags should be used if the branch is to be pushed on a remote repository. To view all this information we first need to get the hash of the tag from the .git/refs/tags directory. Then run the following to see its content.

git cat-file -p <tag-hash>

Viewing the content of annotated tag

Git Tag Command

We can create, modify or delete tags by using the Git Tag command. Let's learn how to use this command.

Creating Tags

There are two types of tags, Annotated and Lightweight. To create an annotated tag we need to use the -a flag but we can create a lightweight tag simply by giving the name of the tag to the command. This will create a new tag at the point where the HEAD is pointing.

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

When creating an annotated tag we need to give a message to that tag. This will help us in understanding what the tag was for. We can use the -m flag to write a message. If the -m flag is not used then Git will automatically open our configured text editor.

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

To create a tag at some previous commit point, we will need the commit hash. We can pass the commit hash to the Git Tag command to create a tag at that point.

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

We can also retag a commit i.e. change the tag that points to a certain commit. This may be done to alter the contents of the tag. This can be done by using the -f flag(force). We can also create multiple tags with the same name with the help of this flag.

Using the -f flag to forcefully update an existing tag.

Viewing Tags

To list all the existing tags we can simply type in the Git Tag command. This will print out the names of all tags

$ git tag

We can also use the -l flag to use glob patterns to specify the tag name.

$ git tag -l "glob-pattern"

If we want to view the details of a specific tag, we can use the Git Show command. For lightweight tags, it will display the details of the object that the tag points to. For annotated tags, additional information like tagger name, email, and the annotated message will also be displayed. The differences in the files involved in that commit are also shown.

$ git show <tag-name>

Difference in the output of Git Show for lightweight and annotated tags

Deleting Tags

We can delete a tag in Git by using the -d flag.

$ git tag -d <tag-name>

Pushing Tags

We can push the tags that we created on our local repository to the remote repository by using the Git Push command. We can do this by using the --tags option to push all the tags on a branch to a remote repository or we just enter the names of the tags that we want to push along with the branch.

$ git push <remote> <tag-name>

Summary

Tags are important tools that developers often use to identify certain points in the history of the project. Most of the time these include different release versions of a project. There are two types of tags - Annotated and Unannotated. Annotated tags contain additional information called metadata whereas unannotated tags are just simple pointers. Annotated tags are usually preferred when we have to share our branches on a remote repository as these tags provide more information. In this tutorial, we also learned about different flags and options that we can use the Git Tag command to play around with tags.



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