How to Checkout Tag in Git

Abdul Jabbar Feb 02, 2024
  1. Create a Tag in Git
  2. Check Out a Git Tag
  3. Use Tag Name to Check Out the Latest Tag in Git
How to Checkout Tag in Git

Git is one of the top Version Control systems used by various teams worldwide. Like other Version Control systems, Git can also tag some particular points in the repository’s history to be marked important.

Commonly it’s used by developers to mark release points or create tags so that they have reference points in their development for the marking purpose.

This article will discuss the basics of Git tags and how we can create Git tags and check out Git tags easily using various commands. We can easily know what it means by the word Tag.

A tag can be explained as a label used to spot a particular commit or push some work in history. We can use it to mark release points (e.g., v58.0).

A tag is similar to a branch in a particular repository, but it can’t be changed. It specifies a particular commit in the history and can not be replaced unless it’s precisely updated.

After tags are created, they don’t have a further history of commits. It is created on the commit that Head is referring to.

When you need to add a mark to remember later about the release or any particular commit, you can add the tag in that commit to remember that later easily.

Create a Tag in Git

For creating a new tag, we will execute the following command.

$ git tag <tag_name>

There are two different tags: annotated tag and lightweight tag. The last-mentioned command example created a lightweight tag.

The difference between both tags is that when we use an annotated tag, we can add some new additional metadata information as we have in our commit previously, such as email address, date of the release, comment related to the release notes & signature of the person who created the release in the team, which is significant for public release for the team.

While Lightweight tags can be considered a bookmark to a commit in a particular repository, they indicate a name and a pointer towards a commit.

Practically, Annotated tags should be used as public, and Lightweight tags should be used as private. The below-listed command will create a new Annotated tag specified with the v1.0 version tag for the future.

git tag -a v1.0

Check Out a Git Tag

For checking out a Git tag, we will use the following command git checkout command, and we have to specify the tag name and branch that has to be checked out to be saved in the local branch.

$ git checkout tags/<tag> -b <branch>

For this, we should have the latest tag list from our remote repository. We will run the command git fetch with the options -all and the -tags mentioned below for fetching tags from our remote repository.

$ git fetch --all --tags

Let’s suppose we have named a tag v1.0 that we have to check out in a branch named release. We have to run the following command for the mentioned purpose to get the desired results.

$ git checkout tags/v1.0 -b v1.0-branch

Now we have successfully checked out the v1.0 tag.

Furthermore, we can check the state of our branch with the help of the command git log.

But for using this command, we should be assured that the HEAD pointer is pointing towards our current annotated tag in the current branch of the repository.

$ git log --oneline --graph

Use Tag Name to Check Out the Latest Tag in Git

Suppose we want to check out the latest Git tag using the topmost tag of our repository. In that case, we must update our repository by fetching the remote tags available in that current repository.

$ git fetch --tags

We have fetched several tags from our remote repository into the local repository through the above command. Then we will fetch the recent tag that is accessible with the command git describe, as mentioned below.

$ tag=$(git describe --tags `git rev-list --tags --max-count=1`)

$ echo $tag
v2.0

Finally, we will proceed to checkout using the git checkout command.

$ git checkout $tag -b latest

We have successfully checked the recent Git tag available in a new branch using the above command in Git.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Tag

Related Article - Git Checkout