How to checkout a tag
Check out this article to create and push a tag in git. Now, we discuss how to download or checkout a tag. You can use the tags keyword in git checkout to download a specific tag.
A branch in git can be associated with many tags. Every tag will represent a version or a bunch of commits till that point in the branch. The whole branch gets downloaded when you checkout a branch. When you download a tag, the branch will get downloaded till the point of commits associated with the tag.
Let us see this using a real scenario as below.
The initial setup is as below. The branch main in the online repository repoone is as shown. It has eight commits, and tag v1.0 is present at the seventh commit.
~/repoone$ git log --oneline
ae8cdca (HEAD -> main, origin/main, origin/HEAD) Changed file five
378e483 (tag: v1.0, tag: describe) Sixth line in newfile
0516dab This is new file
4632c0d Adding empty directory
908d6ac Third commit in main
42296d0 Second commit in main
453424e This is first commit
2673111 Initial commit
To check out the branch main and tag v1.0, use git checkout with the keyword tags, such as git checkout -b <local branch name> tags/v1.0.
~/repoone$ git checkout -b maintagv1.0 tags/v1.0
Switched to a new branch 'maintagv1.0'
You can check the branch name and commits of this new branch. The commits are downloaded only till where the tag is applied.
~/repoone$ git log --oneline
378e483 (HEAD -> maintagv1.0, tag: v1.0, tag: describe) Sixth line in newfile
0516dab This is new file
4632c0d Adding empty directory
908d6ac Third commit in main
42296d0 Second commit in main
453424e This is first commit
2673111 Initial commit
~/repoone$ git branch
main
* maintagv1.0