448
votes

I would like to know how I could clone only one branch instead of cloning the whole Git repository.

4
What a couple others pointed out is very true: unless there are large files committed to some branches and never to others, this isn't actually going to make much of any difference.Cascabel
@Jefromi: It really makes difference when you clone it.. See this link: stackoverflow.com/questions/14682245/…Amol M Kulkarni
@AmolMKulkarni Like I said two years ago, only if some branches contain a lot of data that others don't. The question you linked to doesn't actually say that just one branch is smaller - if all of that enormous size is just in the common history of all branches, cloning one branch will be just as big.Cascabel
This also makes a difference when certain recipients are intended only to see certain branches and their histories.Old McStopher
Super complete answer by VonC here : stackoverflow.com/a/9920956/1579667Benj

4 Answers

798
votes

From the announcement Git 1.7.10 (April 2012):

  • git clone learned --single-branch option to limit cloning to a single branch (surprise!); tags that do not point into the history of the branch are not fetched.

Git actually allows you to clone only one branch, for example:

git clone -b mybranch --single-branch git://sub.domain.com/repo.git

Note: Also you can add another single branch or "undo" this action.

68
votes

You could create a new repo with

git init 

and then use

git fetch url-to-repo branchname:refs/remotes/origin/branchname

to fetch just that one branch into a local remote-tracking branch.

26
votes

--single-branch” switch is your answer, but it only works if you have git version 1.8.X onwards, first check

#git --version 

If you already have git version 1.8.X installed then simply use "-b branch and --single branch" to clone a single branch

#git clone -b branch --single-branch git://github/repository.git

By default in Ubuntu 12.04/12.10/13.10 and Debian 7 the default git installation is for version 1.7.x only, where --single-branch is an unknown switch. In that case you need to install newer git first from a non-default ppa as below.

sudo add-apt-repository ppa:pdoes/ppa
sudo apt-get update
sudo apt-get install git
git --version

Once 1.8.X is installed now simply do:

git clone -b branch --single-branch git://github/repository.git

Git will now only download a single branch from the server.

15
votes

I have done with below single git command:

git clone [url] -b [branch-name] --single-branch