This post will discuss how to clone a specific tag with Git.

1. git checkout

Here, the idea is to clone a repository using the git-clone command and then check out the specific tag using git-checkout.

# clone the remote repository
$ git clone <repository> .
 
# switch to the specific tag
$ git checkout tags/<tagname>

This is demonstrated below:

git tag

 
Note, this will leave the repository in a ‘detached HEAD’ state. This means any commits you make in this state will not impact any branches. If needed, you can create a new branch using the -b option:

git checkout tags/<tagname> -b <branchname>

2. git clone

You can also check out the specific tag with git-clone. It has –branch option, which can also take tags and detaches the HEAD at that commit in the resulting repository.

$ git clone -b <tagname> <repository> .

This is demonstrated below:

git clone -b

 
If you only need the specific tag, you can pass the --single-branch flag, which prevents fetching all the branches in the cloned repository. With the --single-branch flag, only the branch/tag specified by the --branch option is cloned.

$ git clone -b <tagname> --single-branch <repository> .

Alternatively, you can specify the --depth option, limiting the total number of commits to be downloaded to the specified depth. It implies --single-branch by default.

$ git clone -b <tagname> --depth 1 <repository> .

That’s all about cloning a specific tag with Git.