How To Checkout Git Tags

In Git, tags are references that point to a specific point in time and are commonly used to identify release versions of your code. When you are working with Git, it is necessary to build tags to ought reference points in their development.

Tags are utilized to mark specific commits in Git, e.g. release versions. While a branch pointer moves at additional commits are made, a tag remains set on the specified revision.

Moreover, tags are Git objects indicating that they can be checked out like you would check out a branch or a commit for instance. In this short tutorial, we will be discussing how to checkout a tag in Git easily. Along with this, you can also take a look at the checkout of the latest git tag with the help of Git Commands.

Checkout Git Tag using ‘git checkout’ command

To checkout, a Git tag, use the “git checkout” command and specify the tag name as well as the branch to be checked out.

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

Note that you will have to make sure that you have the latest tag list from your remote repository.

To fetch tags from your remote repository, use “git fetch” with the “–all” and the “–tags” options.

$ git fetch --all --tags

Fetching origin
From git-repository
   98a14be..7a9ad7f  master     -> origin/master
 * [new tag]         v1.0       -> v1.0

Let’s say for example that you have a tag named “v1.0” that you want to check out in a branch named “release”.

To achieve that, you would execute the following command

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

Switched to a new branch 'v1.0-branch'

Using this command, you have successfully checked out the “v1.0” tag.

You can inspect the state of your branch by using the “git log” command. Make sure that the HEAD pointer (the latest commit) is pointing to your annotated tag.

$ git log --oneline --graph

* 53a7dcf (HEAD -> v1.0-branch, tag: v1.0) Version 1.0 commit
* 0a9e448 added files
* bd6903f (release) first commit

Awesome!

Now you can start working on your branch starting from the tag you specified earlier.

Checkout latest Git tag

In some cases, you may be interested in checking out the latest Git tag of your repository.

To check out the latest Git tag, first, update your repository by fetching the remote tags available.

$ git fetch --tags

Fetching origin
From git-repository
   98a14be..7a9ad7f  master     -> origin/master
 * [new tag]         v2.0       -> v2.0
 * [new tag]         v1.0       -> v1.0

As you can see, you retrieve multiple tags from your remote repository.

Then, retrieve the latest tag available by using the “git describe” command.

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

$ echo $tag
v2.0

Finally, use the “git checkout” command to checkout the latest git tag of your repository.

$ git checkout $tag -b latest

Switched to a new branch 'latest'

That’s it! You have successfully checkout the latest Git tag available in a new branch.

You can execute the “git log” command to make sure that you are actually developing starting from the new tag.

$ git log --oneline --graph

* 7a9ad7f (HEAD -> latest, tag: v2.0, origin/master, master) version 2 commit
* 98a14be Version 2 commit
* 53a7dcf (tag: v1.0, v1.0-branch) Version 1.0 commit
* 0a9e448 added files
* bd6903f (branch3) first commit

Conclusion

In this tutorial, you have seen how you can easily checkout tags on Git using the “git checkout” command. Also, we have covered the information about checking out the latest Git tags from your repository in case you have multiple tags.

Interested in Git tags? Check our guide on how to delete local and remote git tags.

Leave a Reply

Your email address will not be published. Required fields are marked *