Loading...
GitVCS

Git Tag – All You Need to Know

What is a git tag?

A Git Tag is just a label or name given to a particular commit in git history. It is like a branch which does not change. Unlike branches, tags, after being created, have no further history of commits. ‘

Tag is usually used to mark release points (eg. v1.0.1, v1.0.2, etc.). But it can be used to mark any important point so that we can refer to it in future if needed.

Say, you are working on dev branch in some project. Once the initial version of project is ready to demo, you can create a tag (say v1.0.0) in dev branch. By default, git tag will create a tag on the commit that HEAD is referring.

HEAD is nothing but a reference/pointer that points to the latest commit in the current branch.

Now, some new requirements came and you decided to create a new branch (say, feature/sso_login) out of dev branch and start working on that branch. Once the requirements are done, you will have to merge the feature/sso_login branch with dev branch so that dev branch will have all the latest code. After merging is done, you can create another tag (say, v1.1.0) in dev branch.

Now, if you checkout the tag v1.0.0, you will just have the initial code without sso login feature. This is where tag plays important role. If you would have not created v1.0.0 tag in dev branch, after merging feature/sso_login branch with dev, you would never have been able to checkout the initial version of code. This is why, we should always create tag before and after merging other branches to main/base branch. In our case, dev is the main/base branch.

We can also create tags (by date like release-12-01-2022, release-20-01-2-22 and so on) whenever we are deploying code to production. In this way, we can keep track of deployments. In emergency or due to some reason, if we need to revert back to previous deployment, we can simply checkout the previous tag and then build and deploy.

Types of Tags

There are two types of tags in git –

  1. Lightweight Tags
  2. Annotated Tags

Lightweight tags and Annotated tags differ in the amount of accompanying metadata they store. Annotated tags store extra metadata such as the tagger name, email, and date. This is important data for a public release. 

Create Tag

Git tag can be created using the git tag command. Please not that once you create a tag, you will have to push it to remote repository.

# Create lightweight tag
git tag <tag-name>

Example: 
git tag v1.0.0

# Create annotated tag
git tag -a <tag-name>

Example:
git tag -a v1.0.0

# Create annotated tag with message
git tag -a <tag-name> -m "message"

Example:
git tag -a v1.0.0 -m "Initial Release, Code Frozen"

Tags are recorded in git commit history. After creating a tag, run git log command to view the tag in git history.

git log --oneline

View Tags Or List All Stored Tags

git tag command below to list all the tags that are stored in the current repository.

# List all tags
git tag

# Output will be similar to the below one:
v1.0.0
v1.1.0

# List tags with message
git tag -n

# Output will be similar to the below one:
v1.0.0       Initial Release, Code Frozen
v1.1.0       SSO Implementation

View Specific Tag

git show command is used to view a particular tag. As mentioned earlier, lightweight tags will have less details (meta data) compared to annotated tags.

git show <tag-name>

Example:
git show v1.0.0

Search Tag

If you want to search and list tags with “beta” in their name, use the following command-

git tag -l '*beta*'

Push Tags To remote

When we create tag, it is created and stored locally in out PC. So, it is imperative to push tags to remote repository.

# Push all tags to remote repository
git push origin --tags

# Push a particular tag to remote repository
git push origin <tag-name>

Checkout Tag

We can checkout a tag the way we checkout a branch.

git checkout <tag-name>
git checkout v1.0.0

Create Branch From Tag

We can create a branch from a tag using switch command as shown below-

git switch -c <tag-name> <branch-name>

# Say, create a branch and implement gmail signin on top of v1.1.0 code
git switch -c v1.1.0 gmail_signin

Delete Tag

# Delete Local tag
git tag -d <tag-name>
git tag -d v1.1.0

# Delete remote tag:
git push --delete origin <tag-name>
git push --delete origin v1.1.0

We can also delete remote tags by using refs syntax-

# Delete remote tag using refs syntax
git push origin :refs/tags/<tag-name>
git push origin :refs/tags/v1.1.0

Diff Between Two Tags

git diff command is used to find out the differences between two tags-

git diff <tag-name-1> <tag-name-2>
git diff v1.0.0 v1.1.0

This is show all the modifications that has been done in v1.1.0 as compared to v1.0.0.

Summary

Basic steps to create a tag and push it to remote repository-

# 1. List existing tags
git tag -n

# 2. Create tag
git tag -a <tag-name> -m "message"

# 3. Push a particular tag to remote repository
git push origin <tag-name>

# 4. Check git log to verify if the tag is recorded
git log --oneline

Hope this article helps. Thanks for reading.

Share this article with your friends