1008

I have a local Git repository in ~/local_repo. It has a few branches:

$ git branch
* master
  rails
  c
  c++

To clone the local repository, I do:

$ git clone ~/local_repo new_repo
Initialized empty Git repository in /home/username/new_repo/.git/

The new_repo master branch points to the local_repo master branch, and I can push / pull.

But I am unable to clone another branch. I want to only pull the branch I want (e.g. rails), so that the new repository has a master branch that pushes to and pulls from local_repo's rails branch, by default. How do I accomplish this, or perhaps something similar with local_repo tracking the master local_repo?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Casey Rodarmor
  • 14,878
  • 5
  • 30
  • 33
  • What does `git branch -a` show? – Jakub Narębski Nov 22 '09 at 10:57
  • 5
    Would `git checkout -b newbranch origin/branchiwant` works better? (without the `--track`option) – VonC Nov 22 '09 at 11:18
  • 4
    I think what you are trying to do there is a bad idea. Use different repositories for different projects. Branches are something completely different. – innaM Nov 22 '09 at 18:45
  • @Manni, I was kind of thinking that, since git didn't seem to like what I'm doing. Can you explain why? Is it because branches shouldn't be long lived? – Casey Rodarmor Nov 22 '09 at 21:02
  • @Jakub, `git branch -a` shows the same thing as the `git branch` above. – Casey Rodarmor Nov 22 '09 at 21:03
  • @rodarmor git branch shows the list of active branches & git branch -a shows all the branches after cloning any repo. – Vijay Kumbhar May 31 '12 at 05:54
  • 1
    @rodarmor I think what you are trying to do there is a good idea, and I had exactly this question. – Paul Du Bois Jun 08 '16 at 12:43
  • Its more a matter of opinion. Google keeps all their code in a [single repository](https://www.wired.com/2015/09/google-2-billion-lines-codeand-one-place/). That being said, I agree with @innaM. This type of workflow is difficult to manage especially when trying streamline version control operations. – Jake Henningsgaard Dec 16 '16 at 21:58

27 Answers27

1121

Note: the git1.7.10 (April 2012) actually allows you to clone only one branch:

# clone only the remote primary HEAD (default: origin/master)
git clone <url> --single-branch

# as in:
git clone <url> --branch <branch> --single-branch <folder>

Note:

  • <url> is the URL of the remote repository, and does not reference the branch cloned
  • <folder> is the local folder where you want to clone the repository

You can see it in t5500-fetch-pack.sh:

test_expect_success 'single branch clone' '
  git clone --single-branch "file://$(pwd)/." singlebranch
'

Tobu comments that:

This is implicit when doing a shallow clone.
This makes git clone --depth 1 the easiest way to save bandwidth.

And since Git 1.9.0 (February 2014), shallow clones support data transfer (push/pull), so that option is even more useful now.
See more at "Is git clone --depth 1 (shallow clone) more useful than it makes out?".


"Undoing" a shallow clone is detailed at "Convert shallow clone to full clone" (git 1.8.3+)

# unshallow the current branch
git fetch --unshallow

# for getting back all the branches (see Peter Cordes' comment)
git config remote.origin.fetch refs/heads/*:refs/remotes/origin/*
git fetch --unshallow

As Chris comments:

the magic line for getting missing branches to reverse --single-branch is (git v2.1.4):

git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
git fetch --unshallow  

With Git 2.26 (Q1 2020), "git clone --recurse-submodules --single-branch" now uses the same single-branch option when cloning the submodules.

See commit 132f600, commit 4731957 (21 Feb 2020) by Emily Shaffer (nasamuffin).
(Merged by Junio C Hamano -- gitster -- in commit b22db26, 05 Mar 2020)

clone: pass --single-branch during --recurse-submodules

Signed-off-by: Emily Shaffer
Acked-by: Jeff King

Previously, performing "git clone --recurse-submodules --single-branch" resulted in submodules cloning all branches even though the superproject cloned only one branch.

Pipe --single-branch through the submodule helper framework to make it to 'clone' later on.

Walf
  • 8,535
  • 2
  • 44
  • 59
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 112
    `git clone --branch --single-branch ` works like a charm. – Alexander Oct 01 '12 at 16:01
  • 2
    Yup, I think it's time to update the accepted answer :) This does "work like a charm" – kumarharsh Aug 14 '13 at 07:32
  • 7
    Also, this is implicit when doing a shallow clone. This makes `clone --depth 1` the easiest way to save bandwidth. – Tobu Sep 07 '13 at 19:13
  • 1
    @Tobu and VonC `--depth 1` is unsafe for the user because they may wish to actually push/pull with the new clone. – nmr Jun 04 '14 at 19:48
  • 1
    to get other branches, change the fetch= line in .git/config. Replace the branch name with *. See http://stackoverflow.com/questions/6802145/convert-shallow-clone-to-full-clone, I put a more detailed comment there. – Peter Cordes Jul 29 '14 at 21:38
  • @PeterCordes ok, I have included the unshallow process in the answer for more visibility. – VonC Jul 30 '14 at 05:41
  • Would not the refspec be better if it had a plus in front of it? I.e. `git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*` – Klas Mellbourn Mar 12 '15 at 10:05
  • @KlasMellbourn I don't know if it would be "better", but it would enable `push --force`. – VonC Mar 12 '15 at 10:14
  • 1
    Thanks for the description of how to undo — that's what I actually came here to find out. – Sam Sehnert May 17 '15 at 05:06
  • Thanks @Alexander for the awesome thing: git clone --branch --single-branch woking for me... :-) – Shridutt Kothari Jul 07 '15 at 12:21
  • How to update this `single-branch` repo? it's fetching all the other branches when I try to perform `git pull` :( – Mateo Tibaquira Oct 30 '16 at 23:23
  • @mat if you cloned the repo with the single branch option, the refspec associated to fetch should only be for one branch, not all branches. – VonC Oct 30 '16 at 23:26
  • @VonC Well, this is a huge repo cloned with: `git clone git@github.com:odoo/odoo.git --branch 10.0 --depth 1 --single-branch` and it counts 22641 objects. When I try to pull it counts 300k+ :( The 10.0 branch size is 114.62 MiB and the pull is 600Mb+ – Mateo Tibaquira Oct 30 '16 at 23:43
  • 1
    I was able to update my single branch repo with: `git fetch origin branch --depth 1` then `git reset --hard origin/branch` as I'm not interested on merge anything :) – Mateo Tibaquira Nov 02 '16 at 01:27
  • 4
    Just to spell out Peter Cordes' correction in full, the magic line for getting missing branches to reverse `--single-branch` is `git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*` and `git fetch --unshallow` (git v2.1.4) – Chris Dec 28 '16 at 19:15
  • 1
    @Chris Thank you. I have included your comment in the answer for more visibility. – VonC Dec 28 '16 at 19:34
  • [Here I just added an answer](https://stackoverflow.com/a/63761209/4561887) to expound upon the series of individual and more-common git commands one can run to obtain the `-b branch_name` and `--single-branch` effects while `git clone`ing. – Gabriel Staples Sep 06 '20 at 06:27
  • Is it possible to get back to full repository? I cloned recnetly single branch, I believe like that `git clone --single-branch` and now if I type git checkout origin/ I receive _error: pathspec 'origin/' did not match any file(s) known to git_. The only branch weights about 4 Gb, so it could take a lot of time to re-clone the whole repo again. So how can I turn --single-branch to a regular whole repo clone? – kolyaseg Dec 21 '22 at 10:16
  • 1
    @kolyaseg You can change the refspec ([mentioned here](https://stackoverflow.com/a/57811337/6309)): see the [`git config` command here](https://stackoverflow.com/a/23780060/6309). Or you can add a new branch with [`git remote set-branch --add`](https://git-scm.com/docs/git-remote#Documentation/git-remote.txt-emset-branchesem)), as [mentioned here](https://stackoverflow.com/a/70993964/6309). – VonC Dec 21 '22 at 10:29
697

One way is to execute the following.

git clone user@git-server:project_name.git -b branch_name /your/folder

Where branch_name is the branch of your choice and "/your/folder" is the destination folder for that branch. It's true that this will bring other branches giving you the opportunity to merge back and forth.

Update

Now, starting with Git 1.7.10, you can now do this

git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder
Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81
  • 37
    This works, but it fetches *all* branches. See @frerich=raabe's answer below. – cdunn2001 Mar 17 '12 at 20:33
  • 3
    Thanks you. only add "-b branch_name" in my case. It saved me a lot of time. – PhatHV Aug 27 '12 at 02:51
  • Yes it works perfectly...! I am more of a TFS person and for my personal projects have been using git. W.r.to git branching, I was doing it the wrong way earlier and good to know this easy method and it seems perfect! Thanks again! Already +1'd, if possible I would give +100 :) – k25 Mar 11 '13 at 15:38
  • add --single-branch to reduce bandwidth – xordon May 27 '14 at 23:58
  • 14
    You can also add `--single-branch` to fetch only the history associated with this branch instead of all history & tags the repository contains. – Mickäel A. Jun 18 '14 at 08:11
  • 1
    @AlexNolasco Can you please edit the answer saying what is **/some/folder** ? Thank you – Nabin Sep 03 '16 at 14:04
  • @NabinKhadka i've clarified as suggested – Alex Nolasco Sep 05 '16 at 00:26
115

Using Git version 1.7.3.1 (on Windows), here's what I do ($BRANCH is the name of the branch I want to checkout and $REMOTE_REPO is the URL of the remote repository I want to clone from):

mkdir $BRANCH
cd $BRANCH
git init
git remote add -t $BRANCH -f origin $REMOTE_REPO
git checkout $BRANCH

The advantage of this approach is that subsequent git pull (or git fetch) calls will also just download the requested branch.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
  • Whats the different between your script and this line? `git clone -b $BRANCH $REMOTE_REPO $BRANCH` afik they are the same? – Ian Vaughan Aug 02 '11 at 08:54
  • 12
    @Ian: One difference is that `git clone -b` gives you all remote refs (all remote branches and tags) as `git branch -a` shows. With my script, you just get a single ref. – Frerich Raabe Aug 23 '11 at 10:22
  • 1
    When I try the above (say with $BRANCH="nick"), I get 'fatal: Couldn't find remote ref refs/heads/nick'. Doesn't seem to work for me... – Nick Nov 11 '11 at 16:10
  • 3
    Note that `-t $BRANCH` works even without `-f`, which would fetch immediately. Then, `git fetch origin` would fetch, and `git checkout $BRANCH` would establish the local branch and checkout. That is useful when you need to configure the remote before fetching, e.g. `git config remote.origin.uploadpack=/bin/upload-pack`. – cdunn2001 Feb 23 '12 at 22:02
  • 1
    As I'm on an "old" corporate git (1.6.0.2) I'm bound to above solution. I had same problem as @Nick. Executing git `branch -a` showed `origin/$BRANCH`. Doing `git checkout origin/$BRANCH` fixed this. – dr jerry Jul 11 '14 at 12:14
33

You can try the long-winded way:

mkdir newrepo.git
cd newrepo.git
git init
git remote add origin file:///path/to/original
git fetch origin branchiwant:refs/remotes/origin/branchiwant
git checkout -b branchiwant --track origin/branchiwant

What this does is:

  • Create and init an empty Git repository.
  • Adds the original repository as a remote called origin.
  • Fetches only the branch you require from the remote called origin.
  • Creates and checks out a new branch that is set up to track the source branch you just cloned.

Hopefully that will be something like what you are after.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jkp
  • 78,960
  • 28
  • 103
  • 104
  • The arguments to git remote add are swapped, but I couldn't then get checkout to work. "git checkout origin/rails" gave me "error: pathspec 'origin/rails' did not match any file(s) known to git." and the alternative gave me "fatal: git checkout: updating paths is incompatible with switching branches." – Casey Rodarmor Nov 22 '09 at 10:22
  • @rodarmor: have corrected the git remote command, thanks. Can you paste the output of `git branch -r`? – jkp Nov 22 '09 at 11:54
  • @jkp, Actually `git branch -r` gives no output. – Casey Rodarmor Nov 22 '09 at 21:00
  • @rodarmor: I've corrected the example and this definitely works (tested). HTH (you can accept it once tested it you like ;)) – jkp Nov 23 '09 at 01:34
  • the -b switch is relatively new. Last time I ran it on a debian squeeze it wasn't available, IIRC – sehe Mar 20 '11 at 23:47
  • the last cmd should be: git checkout -b branchiwant --track remotes/branchiwant – KONG Jul 24 '11 at 07:16
  • wow, thank you. You save my day! – Programmer Dancuk Jul 14 '23 at 11:46
32
git clone <url> --branch <branch> --single-branch

Just put in URL and branch name.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jsingh
  • 1,256
  • 13
  • 24
26

You can do it by using the below command:

git clone -b branch_name --single-branch project_url local_folder_to_clone_in
Arulkumar
  • 12,966
  • 14
  • 47
  • 68
nosaiba darwish
  • 1,179
  • 11
  • 13
  • 2
    Worked perfectly, I actually had problem in Source Tree to clone a single branch, thus did it manually – azhar22k Jan 30 '17 at 17:29
20

From git-clone man page:

--single-branch is your friend during clone remember to use with --branch <branch name> or only remote primary HEAD will be cloned (master by default)

Always remember to do Ctrl + F5 to read fresh source, not the one from cache :-) (I didn't so didn't know about this option for long time.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Casey
  • 1,402
  • 1
  • 19
  • 23
10

Clone only one branch. This is the easiest way:

git clone -b BRANCH_NAME --single-branch git@bitbucket.org:___/PROJECTNAME.git
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Cubiczx
  • 1,005
  • 11
  • 10
7

There are ample answers here which mention:

  1. Download 1 branch (with the --single-branch part):

    # (We'll refer to this as "the 1st command" below.)
    # 1. Clone ONLY `branch_name`, then check it out. The `--single-branch` part
    #    means that ONLY `branch_name` is cloned, fetched, pulled, and
    #    tracked. _No other remote branches will be cloned to your PC
    #    whatsoever._
    git clone -b branch_name --single-branch \
    https://github.com/some_project/some_project.git
    

    ...or some version of that, and a few which mention just:

  2. Download all branches (withOUT the --single-branch part):

    # (We'll refer to this as "the 2nd command" below.)
    # 2. Clone ALL remote branches, then `git checkout` the `branch_name`
    #    branch.
    git clone -b branch_name \
    https://github.com/some_project/some_project.git
    

But, I'd like to expound upon these two things a bit and show a more familiar set of equivalent commands so we can see what is happening with each under-the-hood.

Let's assume that you have a remote repo on GitHub at https://github.com/micronucleus/micronucleus.git, with remote branches master and version_2.5 (this is a real example you can actually run right now).

Breakdown of the 2nd command from above:

The 2nd command (git clone -b version_2.5 https://github.com/micronucleus/micronucleus.git) clones ALL REMOTE BRANCHES to your local PC, but then checks out the version_2.5 branch instead of the master branch. That one command is the equivalent of doing this:

git clone https://github.com/micronucleus/micronucleus.git
cd micronucleus  # cd into the repo you just cloned
git checkout version_2.5
# To be pedantic, also delete the local `master` branch since
# technically it won't exist yet since you haven't yet checked
# it out with `git checkout master`, which would create it from
# your locally-stored remote-tracking branch, `origin/master`
git branch -d master

The -b version_2.5 part automatically checked out the version_2.5 branch for us instead of master.

git branch -a shows us that ALL branches, however, were cloned to our local PC. Here you can see our local branch version_2.5, which we are on, plus the locally-stored remote-tracking branches origin/HEAD (which points to origin/master), plus origin/master, and origin/version_2.5:

$ git branch -a
* version_2.5
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/version_2.5

We can also look at what our fetch references are. You can either open up the .git/config file to see them directly, or just run git config remote.origin.fetch:

$ git config remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*

You can see above that our git fetch command (which is also triggered by git pull since that is equivalent to git fetch && git merge) is configured to fetch ALL heads for ALL branches in the origin remote. I'm not an expert on this part, but I believe that's what +refs/heads/*:refs/remotes/origin/* means.

Breakdown of the 1st command from above:

The 1st command (git clone -b version_2.5 --single-branch https://github.com/micronucleus/micronucleus.git) clones ONLY the version_2.5 branch to your local PC, and it also checks it out. That one command is the equivalent of doing this (in the end result at least, except that it also downloads much less data in the beginning since it only clones ONE branch NOT all of them):

git clone https://github.com/micronucleus/micronucleus.git
cd micronucleus  # cd into the repo you just cloned
git checkout version_2.5

# Delete ALL other branches, including remote-tracking ones, which are not the 
# `version_2.5` branch:
# One local branch
git branch -d master
# ALL other locally-stored remote-tracking branches
git branch -dr origin/HEAD 
git branch -dr origin/master

# Fix your `.git/config` file so that `git fetch` does the right thing, fetching
# ONLY the `version_2.5` branch head from the `origin/version_2.5` remote branch:
git config remote.origin.fetch \
"+refs/heads/version_2.5:refs/remotes/origin/version_2.5"

The -b version_2.5 part caused the version_2.5 branch to be checked out instead of the master branch by default (as previously explained above), and the --single-branch part caused:

  1. NONE of the other branches to be cloned to our PC, and
  2. git fetch to be configured such that NONE of the other branches will ever be fetched when we call git fetch or git pull!

This command truly cloned and will fetch only the one branch we wanted, and that's it!

git branch -a shows us that ONLY the version_2.5 branch was cloned and checked out. Here we see by the * which branch is checked-out, and we see also that we have a locally-stored remote-tracking branch for origin/version_2.5:

$ git branch -a
* version_2.5
  remotes/origin/version_2.5

We can also look at what our fetch references are. You can either open up the .git/config file to see them directly, or just run git config remote.origin.fetch:

$ git config remote.origin.fetch
+refs/heads/version_2.5:refs/remotes/origin/version_2.5

You can see above that our git fetch command will only fetch the version_2.5 branch head from the origin/version_2.5 remote branch. That's it! Beware that no other remote branches will ever be fetched.

Summary:

So, now you see that using -b branch_name basically just ensures the branch_name branch is checked-out after the clone, but still clones ALL remote branches, whereas adding also --single-branch ensures that ONLY branch_name is cloned, fetched, pulled, and tracked. No other remote branches will be cloned to your PC whatsoever.

Personally, I prefer the -b branch_name option alone, because I want all branches cloned to my local PC. The one exception might be on a huge, shared mono-repo which has dozens, or even hundreds or thousands of remote branches. In that case, just use -b branch_name --single-branch to clone just the one main branch you care about and be done. Better to download 50 GiB of data for the master branch in a huge mono-repo, for instance, than to download 200 GiB of data so you can have 2000 of your peers' branches they are working on too!

References:

  1. Clone only one branch
  2. How do you stop tracking a remote branch in Git?
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
7
git clone --branch <branchname> <remote-repo-url>

or

git clone -b <branchname> <remote-repo-url>
5

For cloning a specific branch you can do :

git clone --branch yourBranchName git@yourRepository.git
vvvvv
  • 25,404
  • 19
  • 49
  • 81
nPcomp
  • 8,637
  • 2
  • 54
  • 49
5

️ Some Performance Measurements ️

I compared two of the suggested approaches, and found that one is way faster (and smaller) than the other (which is advantageous if your only goal is to clone, and you don't care about much else).

I found that the most important performance indicator is --depth, meaning that VonC's answer is the fastest. Try it yourself:

time bash -cl "git clone --single-branch --depth=1 --branch=$MYBRANCH $MYGITURL"

I tested this with a big project with a long history and many branches, and this approach takes about 6s.

Note that, contrary to what is claimed by several comments in this thread, (at least in a recent git version), this will only checkout the target branch. git branch -a only lists that single branch.

The Runner Up

By comparison, frerich-rabe's approach consistently took 26s:

time bash -cl "git init && git remote add -t $MYBRANCH -f origin $MYGITURL && git checkout $MYBRANCH"

When comparing the two, it is also important to note that, in my case, the target branch is a much slimmed down version of its parent branch. The first approach's download progress bar reflects that reduction in size (< 10MB), while the second does not (> 90MB). (I'm sure, there are more mature methods to measure total download size, but I have not looked into that yet.)

Domi
  • 22,151
  • 15
  • 92
  • 122
4

For cloning a branch of Git you don't have the public key to, use this:

git clone -b <branch> <Git repository URL or clone URL you get from Git repository>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prem S
  • 217
  • 3
  • 8
4

I did it this way

git clone -b <branch_name> url

example :

git clone -b master https://gitlab.com/jhondoe/applicationproject.git

or

git clone -b master git@gitlab.com:jhondoe/applicationproject.git
Ashwini
  • 653
  • 6
  • 7
3

Open the cmd.

cd folder_name  # enter the path where to clone the branch

Just one command:

git clone url_of_projecturltoclone -b branch_name
vvvvv
  • 25,404
  • 19
  • 49
  • 81
dhS
  • 3,739
  • 5
  • 26
  • 55
3

A little late but I wanted to add the solution I used to solve this problem. I found the solution here.

Anyway, the question seems to be asking 'how to start a new project from a branch of another repo?'

To this, the solution I used would be to first create a new repo in github or where ever. This will serve as the repo to your new project.

On your local machine, navigate to the project that has the branch you want to use as the template for your new project.

Run the command:

git push https://github.com/accountname/new-repo.git +old_branch:master

What this will do is push the old_branch to new-repo and make it the master branch of the new repo.

You then just have to clone the new repo down to your new project's local directory and you have a new project started at the old branch.

Aaron Pennington
  • 566
  • 5
  • 12
3

Let us take the example of flask repo. It has 3 branches in addition to master. Let us checkout the 1.1.x remote branch

clone the git repo

git clone https://github.com/pallets/flask

cd into the repo.

cd flask

fetch remote branch 1.1.x

git fetch origin 1.1.x

checkout the branch

git checkout 1.1.x

You will switch to the branch 1.1.x and it will track the remote 1.1.x branch.

3

There are primarily 2 solutions for this:

  1. You need to specify the branch name with -b command switch. Here is the syntax of the command to clone the specific git branch.

    git clone -b <BRANCH_NAME> <GIT_REMOTE_URL>
    

    Example:

    git clone -b tahir https://github.com/Repository/Project.git
    

    The following command will clone the branch tahir from the git repository.The above command clones only the specific branch but fetches the details of other branches. You can view all branches details with command.

    git branch -a
    
  2. You can use --single-branch flag to prevent fetching details of other branches like below:

    git clone -b <BRANCH_NAME> --single-branch <GIT_REMOTE_URL>
    

    Example:

    git clone -b tahir --single-branch \ 
    https://github.com/Repository/Project.git
    

    Now if you do a git branch -a, it will only show your current single branch that you have cloned and not all the branches. So it depends on you how you want it.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Tahir77667
  • 2,290
  • 20
  • 16
  • This isn't quite right. fetching **is** cloning! When you fetch a remote branch, you are cloning that remote branch's contents to your *locally-stored* `remote_name/branch_name` (ex: `origin/branch1`) local remote-tracking clone of that remote branch. So, when you say: "The above command [referring to `git clone -b tahir https://github.com/Repository/Project.git`] clones only the specific branch but fetches the details of other branches.", this is wrong. Actually, _it clones all the branches._ It just _checks out_ the `tahir` branch after cloning all the branches is all. – Gabriel Staples Sep 06 '20 at 04:10
  • That means for your bullet 2, you should also change this ("You can use `--single-branch` flag to prevent fetching details of other branches like below:") to this instead: "You can use the `--single-branch` option to prevent cloning (fetching) all the other branches. Instead, it will just clone (fetch) and then checkout the one branch you specify. See the example below:...". – Gabriel Staples Sep 06 '20 at 04:15
3
git clone --single-branch -b [branch name]  [repository URL]
kamula
  • 197
  • 2
  • 5
2

If you want a shallow clone, you can do this with:

git clone -b mybranch --depth=1 https://example.com/myproject.git localname

--depth=1 implies --single-branch.

thakis
  • 5,405
  • 1
  • 33
  • 33
2

you can use this command to git single branch and rename the folder if you want to keep branched stand alone

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

example

git clone -b mybranch --single-branch git://github/repository.git  project_mybranch
Albaz
  • 905
  • 12
  • 21
2

This should work

git clone --single-branch <branchname> <remote-repo-url>
git clone --branch <branchname> <remote-repo-url>
Ranjeet R Patil
  • 453
  • 6
  • 10
2

Usign Github CLI

If you are cloning a repo using the Github CLI, clone options must be declared differently.

--depth=1

gh repo clone username/repo -- --depth=1

--single-branch

gh repo clone username/repo -- --single-branch

--branch

gh repo clone username/repo -- --branch main --single-branch localDir
lifeisfoo
  • 15,478
  • 6
  • 74
  • 115
1

Similar to what @nosaiba-darwish said here: here

This is what we usually do in our company:

git clone -b <name_of_branch> --single-branch <git_url> folder_to_clone_locally
vvvvv
  • 25,404
  • 19
  • 49
  • 81
1

For the newbies like me, just run the code below

git clone https://gitlab.com/repo/repo.git --branch <name of branch> --single-branch
fill_J
  • 459
  • 5
  • 13
0
git clone --branch {branch-name} {repo-URI}

Example:

git clone --branch dev https://github.com/ann/cleaningmachine.git
vvvvv
  • 25,404
  • 19
  • 49
  • 81
an-apluss
  • 475
  • 4
  • 9
-4

Can be done in 2 steps

  1. Clone the repository

    git clone <http url>
    
  2. Checkout the branch you want

    git checkout $BranchName
    
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Pramod Setlur
  • 811
  • 1
  • 15
  • 27
  • Actually, the `git clone ` part clones **all** the branches to your local computer, NOT just the one branch you wanted cloned. This is because it fetches all branches to your local remote-tracking `remote_name/branch_name` branches, which are true, local clones of those branches. Do `git branch -a` to see all local and local remote-tracking (fetched) branches on your system for proof of this. Using the `--single-branch` option truly keeps those other branches off your system, however, and `git branch -a` will only show the one branch you wanted. – Gabriel Staples Sep 06 '20 at 04:21
  • I didn't downvote you, but this is the reason for your downvotes: people recognize your answer actually clones the entire repo not just the one branch, indicating you don't really know what's happening here, so they downvoted you. – Gabriel Staples Sep 06 '20 at 04:22