DEV Community

Amanpreet Singh
Amanpreet Singh

Posted on • Originally published at blog.amanpreet.dev on

Essential Guide to Mastering Git Tags

Essential Guide to Mastering Git Tags

Introduction

In software development, version control is essential for managing changes, tracking progress, and ensuring the integrity of your codebase. Git, the most popular and widely used is a distributed version control system, that offers various tools to help developers maintain a clean and organized project history. One such tool is Git tags. In this article, we'll explore what Git tags are, why they are important, and how you can use them to enhance your development process.

What are Git Tags?

Git tags are references to specific points in a Git repository's history. Git tags are fixed and immutable, unlike branches, which move as you continue to commit. Tags are typically used to mark important milestones, such as releases, breaking changes (e.g., v1.0.0), making it easier to refer back to them later.

Difference between Git tags and Git branches

Git Branches are mutable references whereas Git Tags are immutable references. In other words, a git tag is created to point to a specific commit and after that does not change, whereas a new commit will automatically update the branch to that commit.

Types of Git tags

There are two types of tags in Git - lightweight and annotated

Lightweight Tags

Lightweight tags as the name suggests are simple references to a commit. They act like a branch that doesnโ€™t change, effectively serving as a quick pointer to a specific commit.

Lightweight tags are also considered as temporary tags as they are not stored in the Git database.

Lightweight tags can be created by using the following command git tag <tagname> as shown below.

git tag v1.0.0
Enter fullscreen mode Exit fullscreen mode

Annotated Tags

Annotated tags are more detailed. They store additional information, including the taggerโ€™s name, email, date, and a tagging message. They are stored as full objects in the Git database. They can also be signed and verified with GPG (GNU Privacy Guard).

Since annotated tags are detailed, they include extra information hence they are created while adding a message such as git tag -a <tagname> -m "message".

git tag -a v1.0.0 -m "Release version 1.0.0"
Enter fullscreen mode Exit fullscreen mode

To create an annotated tag, use the -a flag and include a message with the -m flag together with tagname.

List and View Tags

To list all tags in the repository use git tag command.

git tag

# The above command will output something like this.
v1.0
v1.1
v1.2
v1.3
v1.4
v1.5
Enter fullscreen mode Exit fullscreen mode

To get detailed information on a particular tag use git show <tagname> command.

git show v1.0

# The above command will output something like this.
commit 0e9c504d7ec2ee4cef250dff136826065fd8d9a6 (tag: v1.4)
Author: Aman <aman@example.com>
Date:   Fri Jul 12 19:12:40 2024 -0400
    feat: Add refresh token storage and validation
Enter fullscreen mode Exit fullscreen mode

Above is an example of a lightweight tag displaying the information.

Sharing Tags

By default, tags are not pushed to the remote repository when you push commits. To push tags you need to explicitly use the command git push <remote> <tagname>.

git push origin v1.4
Enter fullscreen mode Exit fullscreen mode

In case there are a lot of tags and you want to push it one go, you can use a --tags flag, here is the command for that. Using a --tags flag pushes all types of tags.

git push origin --tags
Enter fullscreen mode Exit fullscreen mode

To push only annotated tags to the remote use the flag --follow-tags.

git push origin --follow-tags
Enter fullscreen mode Exit fullscreen mode

Deleting Tags

To delete a local tag simply use git tag -d <tagname> as shown in the below command.

git tag -d v1.4
Enter fullscreen mode Exit fullscreen mode

To delete a remote tag use git push <remote> -d <tagname> command.

git push origin -d v1.4
Enter fullscreen mode Exit fullscreen mode

Git Tags Cheat Sheet

Command Description Example
git tag Lists all tags in the repository. git tag
git tag <tagname> Creates a lightweight tag at the current commit. git tag v1.0.0
git tag -a <tagname> -m "<message>" Creates an annotated tag with a message. git tag -a v1.0.0 -m "Initial release version 1.0.0"
git show <tagname> Displays details of an annotated tag, including the message. git show v1.0.0
git tag -d <tagname> Deletes a local tag. git tag -d v1.0.0
git push <remote> <tagname> Pushes a specific tag to the remote repository. git push origin v1.0.0
git push --tags Pushes all local tags to the remote repository. git push --tags
git push <remote> --follow-tags Pushes only annotated tags to the remote repository. git push origin --follow-tags
git push origin -d <tagname> Deletes a tag from the remote repository. git push origin -d v1.0.0
git tag <tagname> <commit> Tags a specific commit instead of the latest one. git tag v1.0.0 9fceb02
git checkout <tagname> Check out a tag (detached HEAD state). git checkout v1.0.0
git tag -s <tagname> -m "<message>" Creates a signed tag using GPG. git tag -s v1.0.0 -m "Signed release"
git verify-tag <tagname> Verifies a signed tag. git verify-tag v1.0.0

Best Practices for Git Tags

  • Use Semantic Versioning: Adopt a versioning strategy like semantic versioning (e.g., v2.1.0) to keep your tags meaningful. You can learn about semantic versioning here.

  • Tag Releases: Always tag a commit when you release a new version of your software. This can be automated via CI/CD pipelines based on your branching strategy.

  • Keep Tags Descriptive: Use annotated tags for significant commits to provide context and information about the tag. Annotated tags bring more information as compared to lightweight tags.

Conclusion

Git tags are a powerful tool in your version control toolkit, offering a simple yet effective way to mark important points in your repository's history. Whether you're managing releases, tracking milestones, or preparing for deployment, Git tags help you maintain a clear and organized workflow.

That's it for today, and congratulations to everyone who has followed this article! You've successfully learned about Git tags. Go and implement them in your projects and make your life as a developer easy! ๐ŸŽ‰

I hope you have learned something new, just as I did. If you enjoyed this article, please like and share it. Also, follow me to read] more exciting articles. You can check out my social links here.

References

Git Documentation: Tagging

Top comments (0)