Cloning A Specific Git Branch

Cloning A Specific Git Branch

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.

The option --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 https://github.com/abhat222/Data-Science--Cheat-Sheet.git

Git Clone Particular Branch Example

For the older versions of git, you can use the following command(s) for the same purpose of cloning only a particular branch.

Example. An example using the above set of commands to clone a specific git branch, the branch master from another git repository URL.

Git Clone Specific Branch Older Versions Example

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.

The difference is just not using the option --single-branch.

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 pdmack-patch-1 branch.

An example of the above command usage is shown in the screenshot given below.

Git Clone Branch Example

Related Articles.

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!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *