174

I cloned a repo using the

git clone -b <branch name> --single-branch <github url> <target directory>

This cloned ONLY this branch, but now I want to switch to the master and other branches. Is there any way besides clearing it out and starting over to clone the rest of the repo that I can undo the --single-branch preference?

7 Answers 7

254
+50

You can tell Git to pull all branches like this:

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

If you look in .git/config, it'll look something like this:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = false
[remote "origin"]
    url = https://github.com/owner/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
    rebase = true

I compared this to a full clone, and saw that the only difference was the "fetch" under [remote "origin"].

Note: I'm running Git version 1.8.2. The config options may have changed if you're running an older version of Git. If my commands don't work, then I'd recommend looking through .git/config to see if you can see something similar.

6
  • 4
    Just wanted to offer you a special thanks because I did a heck of a lot of reading and googling and wasn't able to come across anything like this. Commented Jul 18, 2013 at 13:26
  • 2
    Glad to help. The git command line tool is incredibly powerful (in fact, most of the commands are implemented in terms of other commands), so you can do a whole lot of stuff with it once you understand how the repository is laid out (basically how the .git folder works).
    – sarahhodne
    Commented Jul 18, 2013 at 17:48
  • 1
    Note that this solution will also work if a branch was shallow cloned using the following syntax as well, which experiences the same shortcoming described by OP: git clone --depth=1 --branch branchname [email protected]:foobar/repo.git destinationpath Commented Jan 11, 2018 at 17:35
  • 2
    Wow, this is far from intuitive! It's odd Git doesn't readily have a command to accomplish this. Yeah I got it working... and now I know about the config file int he .git directory too!
    – kashiraja
    Commented May 12, 2018 at 0:11
  • 1
    I was able to do this without manually changing the config: git remote set-branches --add origin '*' and then git fetch. Commented Nov 28, 2022 at 10:18
96

If you want to add a single branch, you can do the following:

git remote set-branches --add origin [remote-branch]
git fetch origin [remote-branch]:[local-branch]

Works with git version 1.9.1

2
  • 8
    This adds multiple fetch = lines to .git/config, very handy when you want to switch between two branches on the remote but not fetch a whole bunch of other branches. Thanks! Commented Apr 16, 2018 at 20:31
  • This does not answer the question.
    – StudioLE
    Commented Jul 8, 2023 at 9:27
28

To add another remote branch to my local repository that was cloned using --single-branch, the following works for me:

git remote set-branches --add origin [remote-branch]
git fetch
git checkout [remote-branch]

You can also use wildcards for [remote-branch], e.g.

git remote set-branches --add origin release-1.*
git fetch
git checkout release-1.5

This works using git version 2.21.1. Other answers suggesting to do git fetch origin [remote-branch]:[local-branch] did not work as it creates the local branch in an untracked state. When running git pull it first tried to merge all the commits of the remote branch to my local one once again.

4
  • 4
    second time i come back to this answer. very useful. thanks!
    – godbout
    Commented May 5, 2020 at 7:18
  • 2
    The most useful answer Commented Jul 13, 2020 at 12:25
  • 1
    I've been here several times xd
    – Nestor
    Commented Jan 4, 2022 at 13:59
  • 1
    Thanks a bunch!
    – Luka
    Commented Mar 15, 2022 at 16:17
9

For me worked:

git remote remove origin
git remote add origin https://*<yourPath>*.git
git fetch
2
  • 1
    Easiest solution without messing with git config. Commented Aug 1, 2023 at 6:02
  • This should also be an accepted answer. Works just fine
    – jujka
    Commented Oct 11, 2023 at 4:18
1

Just add the original repo as a new remote, and work off of there?

git remote add path/to/myrepo myNewOrigin
git fetch myNewOrigin

You can even delete your current 'origin' remote and rename 'myNewOrigin' to 'origin' if you would want to.

From there you can pull/merge/rebase.

0

Just change .git/config file of your local repo, line fetch of [remote origin] section.

Before something like this

[remote "origin"]
    url = https://github.com/owner/repo.git
    fetch = +refs/heads/master:refs/remotes/origin/master

After it will be so

[remote "origin"]
    url = https://github.com/owner/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
0

I initially applied Dominik Pawlak's answer and it worked. But I wasn't able to pull any further changes, after I committed more code into my new branch.

There is another way of resetting single-branch entirely, which hasn't been mentioned here:

git remote remove origin
git remote add origin [email protected]:{yourProject}/{yourRepo}.git
git branch --set-upstream-to=origin/{yourBranch}  {yourBranch}
git pull

This resets everything to the original.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.