Working with Git Tags: A Complete Guide (with Examples)

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction to Git Tags

Tagging in Git is one of the key concepts that help developers mark specific points in a repository’s history as important. Typically, people use this functionality to mark release points (v0.1, v1.0, etc.). In this tutorial, we’ll cover everything you need to get started with tagging in Git, including creating, listing, pushing, and deleting tags.

Types of Tags

In Git, there are two types of tags: lightweight and annotated. Lightweight tags are like bookmarks, a pointer to a specific commit. Annotated tags, however, are stored as full objects in the Git database. They can contain the tagger’s name, email, date, and have a tagging message.

Creating Tags

Creating an Annotated Tag

git tag -a v1.0 -m "Initial release version"

The above command will create an annotated tag named ‘v1.0’. Replace ‘v1.0’ and the message with your desired tag name and message.

Creating a Lightweight Tag

git tag v1.0-lite

This creates a lightweight tag named ‘v1.0-lite’.

Listing Tags

git tag

This command lists all the tags in a repository.

Tagging Previous Commits

You can also tag commits after they’ve been made by specifying the commit checksum:

git tag -a v0.9 9fceb02

Here, ‘9fceb02’ is the checksum of the commit you wish to tag. Replace it with your own commit ID.

Pushing Tags to Remote

By default, the ‘git push’ command does not transfer tags to remote servers. Tags have to be explicitly pushed.

Pushing a Single Tag

git push origin v1.0

Pushing All Tags

git push origin --tags

This will push all your tags to the remote repository.

Checking Out Tags

To checkout a tag, you can create a new branch:

git checkout -b [branchname] [tagname]

For example:

git checkout -b version1 v1.0

This creates a new branch named ‘version1’ from the tag ‘v1.0’.

Deleting Tags

Deleting a Local Tag

git tag -d v1.0

This will delete the tag ‘v1.0’ from your local repository.

Deleting a Remote Tag

git push --delete origin v1.0

To remove a tag from the remote repository, use the above command.

Conclusion

In this guide, we covered the essentials of working with Git tags from creation to deletion, and how to manage tags in the context of a Git repository. With the examples provided, you should have a solid foundation for tagging important milestones and releases in your software’s lifecycle. Remember, tags are snapshot references that could be crucial in identifying specific versions of your code, so use them wisely and judiciously in your development process.