Home Software Engineering How To Checkout Git Tags

How To Checkout Git Tags

by schkn

When working with Git, it is quite common for developers to create tags in order to have reference points in your development.

Tags are created in order to have references to release versions for example.

Furthermore, tags are Git objects meaning that they can be checked out like you would check out a branch or a commit for example.

In this tutorial, we are going to see how you can checkout Git tags easily.

Checkout Git Tag

In order to checkout a Git tag, use the “git checkout” command and specify the tagname 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”.

In order 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.

In order to checkout 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 in order 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 learnt how you can easily checkout tags on Git using the “git checkout” command.

You also learnt more about checking out the latest Git tags from your repository in case you have multiple tags.

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

If you are interested about Git or about software engineering, we have a complete section dedicated to it on the website, so make sure to check it out!

Checkout Git tags

You may also like

8 comments

Links 2311/2019: X.Org Server 1.20.6 and GNU Health 3.6.2 | Techrights November 23, 2019 - 7:28 pm

[…] How To Checkout Git Tags […]

Reply
Math January 8, 2020 - 12:57 pm

Thanks, this was very useful.

Reply
schkn January 11, 2020 - 8:43 am

Thank you!

Reply
How To List Git Tags – devconnected February 23, 2020 - 10:15 am

[…] that you know that some tags are available remotely, you may want to fetch your tags in order to list them […]

Reply
Andreas November 17, 2021 - 4:31 pm

When you write that you want to checkout a branch named ‘release’ …

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

You check out a branch like so

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

creating branch v1.0-branch

Reply
sbub December 7, 2021 - 3:44 pm

awesome, I loved that assignment of the git command return value to a separate variable !

Reply
Jacques Pyrat May 4, 2022 - 2:15 pm

Hi,

How do you swith to the next tag ?
Because, if I run the script again, I get :
fatal: A branch named ‘latest’ already exists.

Reply
Arul November 24, 2022 - 6:12 am

Just would like to add this here ..
To push the created Tag name to remote:
`git push origin `

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.