Checking out tags in Git allows you to switch your working directory to the state of the codebase at the time the tag was created. Tags are often used to mark release points (e.g., v1.0
, v2.0
), making it easy to switch between different versions of the code. This guide will walk you through how to checkout specific tags, work with remote tags, and various other operations related to Git tags.
Listing all of the available git tags
Before checking out a tag, it’s helpful to now which tags exist in the first place.
First run:
git fetch --tags
This will fetch all available tags from the remote, allowing you to see the full list of tags available to you.
In order to see a list of all of the existing tags in your repo run:
git tag
In order to search for specific tags you can also apply a shell wildcard pattern match with the -l
flag:
git tag -l <pattern>
This will output a list of all the tags that match the supplied pattern. If you supply multiple patterns, the command will output a list of all tags that match any of these patterns.
Basic tag checkout
Git checkout tag: To checkout a tag, use the
git checkout
command followed by the tag name:git checkout <tag-name>
This command switches your working directory to the state of the specified tag.
Git checkout a tag as new branch: If you want to make changes starting from a tag, it's best to checkout the tag into a new branch:
git checkout -b <new-branch-name> <tag-name>
Working with remote tags
Git checkout tag from remote: To checkout a tag that exists in the remote repository but not locally, first fetch the tags:
git fetch --tags
Then, checkout the tag using the
git checkout <tag-name>
command.Git checkout remote tag as branch: After fetching tags, you can create a new branch from a remote tag similarly to how you would with a local tag:
git checkout -b <branch-name> <tag-name>
Advanced tag operations
Git checkout latest tag: To checkout the most recent tag, first fetch the tags from remote:
git fetch --tags
Then find the latest tag with a combination of the
git describe
andgit rev-list
commands. You can save this tag to a local variable for use in the next step:latestTag=$(git describe --tags $(git rev-list --tags --max-count=1))
Once you have the latest tag, check out as normal using the variable we just created:
git checkout $latestTag
Git Checkout tagged Version/branch/commit: These operations are fundamentally similar, as they involve checking out a specific state identified by a tag. Use the
git checkout <tag-name>
command for a version or commit, and if you want to start a new branch from a tagged commit, usegit checkout -b <new-branch-name> <tag-name>
.
Handling issues and special cases
Git checkout tag without detached HEAD: When you checkout a tag directly, Git puts your repository in a "detached HEAD" state. To avoid this, checkout the tag into a new branch:
git checkout -b <new-branch-name> <tag-name>
For further reading on this state, please see the guide on resolving a detached HEAD state.
Git checkout tag not working: If you encounter issues checking out a tag, ensure the tag exists (
git tag
) and that you have fetched the latest tags from the remote repository (git fetch --tags
).
Miscellaneous
- GitPython Checkout Tag: If you're using GitPython, a Python library to interact with Git repositories, you can checkout a tag programmatically:
repo = git.Repo('<path-to-repository>')git = repo.gitgit.checkout('<tag-name>')
- GitHub checkout specific tag: While GitHub does not have a direct "checkout" feature in the web UI, you can clone the repository locally and then checkout the tag using the command line tools as described above.
For further reading please see the official git documentation on tagging.