How To Checkout Git Tags – devconnected
Excerpt
Checkout Git tag easily using the git checkout command and checkout latest tags from your Git repository with git describe.
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!