How to Download a Specific Tag With Git
This article illustrates how you can clone a repository at a specific tag version. Git tags signify an important milestone in code production.
If you have a remote repository with tags and are wondering how you can clone the repo and reach a specific tag, this article has you covered.
Download a Specific Tag With Git
For easier context, we will use an example to illustrate the concept.
Assuming this image below represents that remote repository’s tags, how do we clone the repo at v0.0.4
?
There are several ways of doing this. You can use the git clone
command or the git clone
and the git checkout
commands combined.
Let’s start with the git clone
command.
the git clone
Command
We can pass a tag as an argument to the git clone
command to clone and detach our HEAD
to move it to the commit at the v0.0.4
tag. Below is an illustration.
$ git clone -b v0.0.4 https://github.com/user/repo.git
By running the command below, we can create a new branch of this tag.
$ git checkout -b Dev
the git checkout
Command
Alternatively, we can clone the repository and move our HEAD
ref to the v0.0.4
tag.
To clone the repo, we will run:
$ git clone https://github.com/user/repo.git
Once the clone is done, we can move HEAD
to the commit at v0.0.4
, as illustrated below.
$ git checkout tags/v0.0.4
This will switch us into detached HEAD
mode. We can run the git checkout
command to create a new branch based on our tag.
$ git checkout -b newbranch
This can be condensed into one command, as shown below.
$ git checkout tags/v0.0.4 -b newbranch
In conclusion, the methods discussed above end up cloning the whole Git repository. One is a shortcut for the other.
Your choice depends on your preference since both methods accomplish the same thing.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn