I would like to know how I could clone only one branch instead of cloning the whole Git repository.
-
3What 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.– CascabelJan 27, 2011 at 6:35
-
3@Jefromi: It really makes difference when you clone it.. See this link: stackoverflow.com/questions/14682245/…– Amol M KulkarniFeb 4, 2013 at 8:04
-
6@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.– CascabelFeb 4, 2013 at 8:25
-
2This also makes a difference when certain recipients are intended only to see certain branches and their histories.– Old McStopherAug 10, 2014 at 9:30
-
2Super complete answer by VonC here : stackoverflow.com/a/9920956/1579667– BenjApr 26, 2016 at 8:46
4 Answers
From the announcement Git 1.7.10 (April 2012):
git clonelearned--single-branchoption 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.
-
7Thank you,! If --single-branch gives you an error just remove that and keep the -b :)– BraunsonMar 8, 2013 at 17:20
-
26@Brauson if you just put -b then you clone all the branches, and after it checkout that branch. This is not the result expected. So I recommend you, if it is possible update git to lastest (or >=1.7.10) and the command wouldn't give a error.– shakaranMar 9, 2013 at 1:23
-
6Anyone know how to clone two branches - or add another single branch to the single branch cloned repo? Sep 26, 2013 at 10:55
-
2Just a tidbit, if you are seeing any access related problems use https URL instead of git@ URL(i.e ssh URL)– phoenixNov 18, 2014 at 20:07
-
2does
--depth=1help here? I hope it does, because I only want to retrieve on commit and no extra files– user11810894Aug 1, 2019 at 21:36
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.
-
3Did you replace "branchname" with the name of the branch you want? Jan 26, 2011 at 23:53
-
1@Xcode: If you do this, you might want to do
git remote add origin <url>, so that you'll get a remote just as if you'd cloned. (You could then go edit the refspec in .git/config to avoid fetching it all.)– CascabelJan 27, 2011 at 6:34 -
3Using git fetch directly will not add a remote. I just used: git init ; git remote add origin git@github.com:... ; git fetch origin <branch>:refs/remotes/origin/<branch> ; git checkout <branch> Feb 28, 2012 at 14:49
-
1@hugemeow: You could use
git pull, but the question was how to clone one branch.git pullwill also merge that branch into your local branch, which may or may not be desired. And ifgit remote -vhas no output, then I guess you have no remotes. Oct 17, 2012 at 20:22 -
1It maybe worth pointing out that you have to replace both instances of "branchname" with the actual name of your branch. Apr 24, 2014 at 5:07
“--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.
-
3+1 for quick instructions of how to update git in older versions of Debian distributions. Oct 29, 2013 at 19:10
-
Small addition: To get the add-apt-repository command, install the software-properties-common package, I also had to install python-software-properties. (I know this is an old answer but it's still relevant; at least it was for me!).– GraftakFeb 26, 2018 at 8:40
I have done with below single git command:
git clone [url] -b [branch-name] --single-branch