This is a detailed tutorial on cloning a specific git branch. Learn to duplicate a single branch with git without any additional data from other branches.
How To Clone A Specific Git Branch?
Sometimes, you might be wanted to clone just a single and particular git branch. The default git clone command with
--branch the option will actually clone the complete git repository and will check out to the specific branch whose name is followed by the
--branch option. But the thing here is we do not want to even the data of any other branch. The purpose here is to just clone a very specific git branch and only the data associated with that specific branch.
The following command will solve this purpose. This command will only fetch the branch whose name is mentioned in the command ignoring all other branches and their data.
git clone --single-branch --branch <branchname> <remote-repo-url>
--single-branch does the job of selecting the specific branch but make sure that you can make use of this option in git version 1.7.10 and above.
Example. The following git command clones only the branch named
revert-28-master from the git URL
git clone --single-branch --branch revert-28-master https://github.com/abhat222/Data-Science--Cheat-Sheet.git
For the older versions of git, you can use the following command(s) for the same purpose of cloning only a particular branch.
git remote add -t <branchname> -f origin <remote-repo-url>
git checkout <branchname>
Example. An example using the above set of commands to clone a specific git branch, the branch
master from another git repository URL.
git remote add -t master -f origin https://github.com/google/eng-practices.git
Cloning repository and Checking out to a particular branch
You can also clone the entire repository and can checkout to a particular branch directly with a single command. In case, just use the following git command.
git clone --branch <branchname> <remote-repo-url>
The difference is just not using the option
Note. Cloning the entire repository and then checking out to a particular branch is not a good idea in case if you just want to work with that specific particular branch and have nothing to do with the rest of the branches and their data on the repository. But it’s a good idea in the case, for now, you just want to checkout to a particular branch but later may require to switch to other branches.
Example. The following command illustrates the use of the git clone command to clone the entire repository and then checking out to
git clone --branch pdmack-patch-1 https://github.com/kubeflow/kubeflow.git
An example of the above command usage is shown in the screenshot given below.
- Rename Git Branch – Locally & Remotely
- Git Set UpStream – What Does It Do & How To Use It?
- Git Commands Explained With Examples
I hope you found this guide useful. Do share it with others who might find it useful as well. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!