How to Use Git Tags

This article goes over what is a Git tag and how to use Git tags, including creating, listing, and deleting.

What is a Git Tag

Tags are references that point to a specific point in Git history. They capture a historical point for a marked version release (such as v1.0.0). A tag is like a branch that does not change.

Create a Tag

When releasing a new software version using Git flow, a tag is usually created (automatically if via the CI process in the CI/CD pipeline).

The bash script below shows how to create and push a tag to the remote server. #check out the branch git checkout develop

#create a tag off the head of the current branch
 git tag -a v1.0.1 -m "version 1.0.1"
#push the tag to remote server
git push origin v1.0.1
#verify that the tag has been pushed to the remote server
git tag -l "v1.0.1"

Create a Branch based on a Tag

After releasing a software version (say v1.0.1) to production, sometimes bugs would be discovered against that version. To create a “hot-fix” version based on v1.0.1, a common practice is to create a branch based on the release tag (“v1.0.1”), then apply bug fixes to that branch, then create a hot-fix tag based on the head of that hotfix branch.

After you have cut many releases (which result in the release tags) or tested the CI/CD process (which results in the test tags), you may want to delete the git tags. The bash script shows how to delete unwanted git tags:

# create a branch off a tag
git checkout -b v1.0.1-hotfix-branch v1.0.1-tag
# make changes to the local hotfix branch
git add 
git commit 
# push the hostfix branch to the remote git repo
git push origin
# create a tag at the head of the hotfix branch at the local repo 
git tag -a v1.0.1-hotfix-tag
# push the tag to the remote repo
git push origin v1.0.1-hotfix-tag

Clean up Git Tags

# delete a tag from local repo
git tag -d v1.0.1
# apply the deletion at the remote repo
git push origin :refs/tags/v1.0.1
# confirm that the tag has been removed
git tag