Sling Academy
Home/DevOps/Working with Git Tags: A Complete Guide (with Examples)

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

Last updated: January 27, 2024

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.

Next Article: Understanding Git Reflog: The Ultimate Guide

Previous Article: How to Clean Up Git History with Interactive Rebase

Series: Git & GitHub Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide