495

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

Ry-
  • 218,210
  • 55
  • 464
  • 476
max_
  • 24,076
  • 39
  • 122
  • 211
  • 3
    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 Jan 27 '11 at 06:35
  • 4
    @Jefromi: It really makes difference when you clone it.. See this link: http://stackoverflow.com/questions/14682245/why-is-the-size-of-the-forked-repository-so-huge-on-github – Amol M Kulkarni Feb 04 '13 at 08: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. – Cascabel Feb 04 '13 at 08:25
  • 2
    This also makes a difference when certain recipients are intended only to see certain branches and their histories. – Old McStopher Aug 10 '14 at 09:30
  • 2
    Super complete answer by VonC here : http://stackoverflow.com/a/9920956/1579667 – Benj Apr 26 '16 at 08:46
  • Note: this work with submodules too now (Git 2.26, Q1 2020). See the last section of [How do I clone a single branch in Git?](https://stackoverflow.com/a/9920956/6309) – VonC Mar 06 '20 at 08:40

4 Answers4

868

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.

Community
  • 1
  • 1
shakaran
  • 10,612
  • 2
  • 29
  • 46
  • 7
    Thank you,! If --single-branch gives you an error just remove that and keep the -b :) – Braunson Mar 08 '13 at 17:20
  • 28
    @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. – shakaran Mar 09 '13 at 01:23
  • 6
    Anyone know how to clone two branches - or add another single branch to the single branch cloned repo? – Billy Moon Sep 26 '13 at 10:55
  • 2
    Just a tidbit, if you are seeing any access related problems use https URL instead of git@ URL(i.e ssh URL) – phoenix Nov 18 '14 at 20:07
  • @BillyMoon did you find an answer to cloning only a set of branches (not just one and not all)? please share if you did – geekay Apr 27 '15 at 09:59
  • @geekay alas - I never did :( – Billy Moon Apr 27 '15 at 17:56
  • @Billy Moon This question explains how to add another branch: [How do I “undo” a --single-branch clone?](http://stackoverflow.com/questions/17714159/how-do-i-undo-a-single-branch-clone) – Samuel May 21 '15 at 19:35
  • This solved my problem as well. Thanks! Upvoted. :) – Dee J. Jun 30 '17 at 05:59
  • I've observed on a big repository hosted on gitlab that when I use the `--single-branch` option, then the stage "remote: Compressing objects" is waaay slower that when I'm not using it. I suppose this is due to some caching on the remote side. So if you want to save space on disk then this option is useful, however if you try to reduce the duration of the clone operation, then it seems it doesn't help. – vadipp Oct 10 '17 at 10:53
  • Tnks, worked for me. – Gabriel Ribeiro Silva Dec 27 '18 at 16:44
  • 2
    does `--depth=1` help here? I hope it does, because I only want to retrieve on commit and no extra files –  Aug 01 '19 at 21:36
  • @user11810894 I get that your account is deleted, but --depth=1 in my experience only clones the master branch – TheTechRobo the Nerd Jun 17 '20 at 13:39
  • This worked for me. Would like to add the newly cloned branch did not show up after running `git branch` until after checking out to it. – Tom Dixon Sep 12 '20 at 19:21
70

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.

yegor256
  • 102,010
  • 123
  • 446
  • 597
Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • 3
    Did you replace "branchname" with the name of the branch you want? – Lily Ballard Jan 26 '11 at 23:53
  • You now have a remote-tracking branch containing the contents of the branch from the remote repo. You can do whatever you want with it, including creating a local branch from it, or resetting your current branch to it, or anything else. – Lily Ballard Jan 27 '11 at 00:15
  • I'd advise you to try cloning the whole repository and see how much it increases your .git subdir. My guess would be that it is neglicible in increase, even on very large projects. – I GIVE CRAP ANSWERS Jan 27 '11 at 00:51
  • 1
    @Xcode: If you do this, you might want to do `git remote add origin `, 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.) – Cascabel Jan 27 '11 at 06:34
  • @pringlized is the simplest solution. – Hedgehog Feb 01 '12 at 00:14
  • @Hedgehog: Except pringlized's solution is wrong. I will comment on his as to why. – Lily Ballard Feb 01 '12 at 00:46
  • 3
    Using git fetch directly will not add a remote. I just used: git init ; git remote add origin git@github.com:... ; git fetch origin :refs/remotes/origin/ ; git checkout – Eric Darchis Feb 28 '12 at 14:49
  • @KevinBallard why not use `git pull url-to-repo branchname:refs/remotes/origin/branchname` – hugemeow Oct 17 '12 at 16:24
  • @KevinBallard git remote -v ,and none is returned , why? – hugemeow Oct 17 '12 at 17:58
  • 1
    @hugemeow: You could use `git pull`, but the question was how to clone one branch. `git pull` will also merge that branch into your local branch, which may or may not be desired. And if `git remote -v` has no output, then I guess you have no remotes. – Lily Ballard Oct 17 '12 at 20:22
  • 1
    It maybe worth pointing out that you have to replace both instances of "branchname" with the actual name of your branch. – infiniteloop Apr 24 '14 at 05:07
  • +1, Nice Answer in case your git version < 1.7.10. Just one point, after the last step, when you get the `* [new branch] master -> origin/master` (supposing you are on master) maybe you want to do a `git merge origin/master` – user9869932 Jun 24 '15 at 17:10
  • Actually this solution don't add the git origin, but for some use case it is better (in my case for instance), so thank you for posting that – Alburkerk Mar 17 '17 at 10:24
  • This is the answer I wanted for an old `git` program to fetch an old branch of a project because everything newer is too new. – Cupcake Protocol Sep 05 '18 at 20:30
28

--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.

Waqas
  • 3,763
  • 1
  • 30
  • 18
  • 3
    +1 for quick instructions of how to update git in older versions of Debian distributions. – Dielson Sales Oct 29 '13 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!). – Graftak Feb 26 '18 at 08:40
23

I have done with below single git command:

git clone [url] -b [branch-name] --single-branch
pRaNaY
  • 24,642
  • 24
  • 96
  • 146