Frequently asked questions around Git and Version Control.
How to Checkout Tags in Git
In Git, tags are used to mark specific commits, e.g. release versions. This is also the big difference between tags and branches: while a branch pointer moves when additional commits are made, a tag remains fixed on the specified revision.
In this short article, we'll now take a look at how to checkout a tag in Git.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Using git checkout with Tags
git checkout command is mainly used for handling branches, but it can also be used for tags:
$ git checkout v2.0
By providing the tag's name as a parameter, Git will checkout that tag's revision.
However, this might not be what you actually wanted, because your local repository is now in a "Detached HEAD" state! This means that the HEAD pointer is currently NOT on a branch, but on a specific revision. If you create additional commits in this state, they will not be associated with any branch (because no branch is currently checked out)! In such a scenario, it's very easy to lose your new commits!
It's much more likely that would like to create a new branch, based on the tag's commit. You can simply add the
-b flag and provide a name for the new branch:
$ git checkout -b new-branch v2.0
You will then have a brand new branch named "new-branch" that's based on the revision that the "v2.0" tag points at.
Checking Out Tags in Tower
In case you are using the Tower Git client, you can simply drag & drop the tag in Tower's sidebar to create a new branch from it and check it out:
- More frequently asked questions about Git & version control