Signup/Sign In

How to Checkout Git Tags

In Git terminology, checkout means to navigate from one version of an object to another. This object can be a branch, a commit, a file, or even a tag. A tag in Git is just a way of marking important points in the history of our projects. In most cases, these points will be software release versions. Let's learn how to checkout tags in Git.

Git Tags

  • Tags in Git are just a way of marking specific points in the history of our project. These tagged points can then be referenced in the future.
  • There are two types of tags in Git - Annotated, and Lightweight. Lightweight Tags or Unannotated Tags are simple pointers that point to some other Git object like commit. Annotated Tags on the other hand are more than just pointers. They are capable of storing additional information like the object that is tagged, the tagger details, a message, etc.
  • Tags can also be pushed and pulled from the remote repositories so that all the collaborators are aware of the changes that someone has marked.

Tags

Checking Out Tags

Creating a tag is fine but it won't add any value to our project if we can't go back to that tagged object and analyze the changes that we made. The Git Checkout command can be used to check out the tags that we have created.

First, we may need to fetch new tags from the remote repository that were added by other developers. This can be accomplished by using the --tags option with the Git Fetch command.

$ git fetch --tags <remote-name>

Next, we will need the name of the tag that we want to checkout. We can see the tag names by using the Git Tag command. We can also use the -l flag to search for tags by using Glob Patterns.

$ git tag 
$ git tag -l "glob-pattern"

Output:

Viewing the existing tags

Viewing tags using glob patterns

Now we know the names of the tags that we want to checkout. To simply checkout a tag use the following Git Checkout command.

$ git checkout <tag-name>

A thing to note here is that the above command will make our repository go into the Detached HEAD state. What it means is that our HEAD no longer points to our currently checked-out branch but instead, it points at a specific commit. In this state, we can view the previous changes and experiment with things and create new commits but these commits will not be added to any branch.

Checking out a tag in detached head state

To avoid the Detached HEAD state we can create a new branch based on that tag and then start experimenting with things. To do this we use the -b flag with the Git Checkout command.

$ git checkout -b <new-branch-name> <tag-name>

Creating a new branch based on the tag

To see the logs.

Git Log command shows that the branch is based on the same commit as the tag

Summary

Tags are an important part of any Git Repository. They are used to mark release versions of our project and help us in understanding these versions better. We can checkout or navigate to a tag by using the Git Checkout command. We can either checkout the tag in the Detached HEAD state or we can create a new branch based on that tag.