Introduction to Tagging in Git

woman sitting while operating macbook pro
Reading Time: 2 minutes

Like most Version Control Systems, Git has the ability to tag specific points in a repository’s history as important. Generally, people use this feature to mark version points (v1.0, v2.0, etc.). In this blog, you will learn about git tagging, how to list existing tags, how we can create and delete tags,  and the different types of tags.

Listing of tags

  • For listing all tags we have this command –
    git tag (-l or –list)
    Through this command, you will get the list of all tags present in that repository.
$git tag -l

 rel-5.1.1
 rel-5.1.2
 rel-6.1.1
 rel-6.1.2
 ...
  • If you wish to apply filters on listing the tags, you can do it through the below command
    git tag -l “rel-*”
    This will give you all the tags that start with rel as a prefix.
 $git tag -l "rel-6.*"

  rel-6.1.1
  rel-6.1.2
  rel-6.1.3
  .....

Creating tags

Git supports two types of tags: lightweight tags and annotated tags. They both allow you to reference a specific commit in your repository, but they differ in the amount of metadata they can store.

Annotated tags

Annotated tags store additional metadata as complete objects in the Git database, such as author name, release notes, tag message, and date. 
git tag -a rel-5.2.1 -m “first tag of 5.2 release”

The – m Specifies the tag message, to be stored with the tag. If you don’t specify a message for an annotated tag, Git launches your editor so you can enter it.
If you execute the git show command you can see all the tag-related data.

$ git show rel-5.2.1

tag rel-5.2.1
Tagger: Alan Joe <joe@alan.cc>
Date:   Fri Mar 09 18:56:52 2023 -0700

first tag of 5.2 release

commit ca82a6dff81gg897662007202690a93763949
Author: Alan Joe <joe@alan.cc>
Date:  Fri Mar 09 18:56:52 2023 -0700

Lightweight tags

Lightweight tags are the easiest way to add tags to a git repository, as they only store the hash of the commit they refer to. They are created without the -a, -s, or -m options, * containing no additional information.

You can create a new lightweight tag by executing the below command-
git tag v1.2.3
This time, if you run git show on a tag, you won’t see the additional info about the tag. This command only displays commits:

$ git show v1.2.3

commit ca596avpo89gg342007202690a93763949
Author: Matt Cole <cole@matt-mail.com>
Date:   Fri Mar 09 18:56:52 2023 -0700

Deleting Tags

For deleting the tags on your repository you can use the below commands

Local Repository

For deleting the tag from the local repository you can run this command
git tag -d <tagname>

$ git tag -d rel-5.2.1

Deleted tag 'rel-5.2.1' (was e7d5add)

Remote Repository

The above command will not delete the tag for the remote repo, for that you can use below two commands.
git push origin :refs/tags/<tagname>
git push origin –delete <tagname>

Conclusion

In this blog, we have learned about git tagging and how to create, list and delete tags. And also looked into the types of tags i.e lightweight and annotated tags. For more such blogs you can click here
Hope you enjoyed the blog. Thanks for reading.

References

https://www.atlassian.com/git/tutorials/inspecting-a-repository/git-tag
https://www.freecodecamp.org/news/git-tag-explained-how-to-add-remove/