How to Clone Specific Tag in Git

Stewart Nguyen Feb 02, 2024
How to Clone Specific Tag in Git

In this article, we’ll learn how to clone a specific tag from a remote repository.

To do so, we’ll use the command git clone -b <tag> --single-branch <remote_repository>.

  • The -b option accepts a tag or a branch you want to clone.
  • The --single-branch option indicates that just the tag supplied by option -b will be cloned to local. All other remote branches/tags will be ignored.

Git does not create a new branch for us after cloning; in fact, it just refers to the tag’s SHA.

It’s our responsibility to establish a new branch from the tag’s SHA using git switch -c <new-branch-name>.

$ git clone -b v1.0.0 --single-branch git@github.com:stwarts/git-demo.git && cd git-demo
Cloning into 'git-demo'...
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 13 (delta 1), reused 8 (delta 0), pack-reused 0
Receiving objects: 100% (13/13), done.
Resolving deltas: 100% (1/1), done.
Note: switching to '9265e3bd97863fde0a13084f04163ceceff9a9d0'.

You are in a `detached HEAD` state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you have created, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

$ git switch -c branch-off-from-tag-v1.0.0
$ git branch
* branch-off-from-tag-v1.0.0

Related Article - Git Clone

Related Article - Git Tag