1

I have cloned a project from Github from branch, say, br1

git clone --branch br1 https://www.github.com/project/project /opt/project/

I would like to switch to branch br2 as if I had did this from the beginning:

git clone --branch br2 https://www.github.com/project/project /opt/project/

I don't want all the branches locally, only the one I use since I am on a ressource limited VPS.

Can I do something like this (pseudo code for the first line)?

git fetch origin/br2
git branch -D br1

EDIT: here is my .config

# cat .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = https://www.github.com/project/project
[branch "br1"]
    remote = origin
    merge = refs/heads/br1
lalebarde
  • 1,684
  • 1
  • 21
  • 36

1 Answers1

2

The simplest approach is to:

  • delete your current repo
  • do the clone again

    git clone --branch br2 https://www.github.com/project/project /opt/project/
    

That will limit the network activity to a minimum, like a custom fetch would (but the clone is simpler than the fetch here)

If you want to test the fetch option:

git remote rm origin
git remote add -t branch2 origin remote-url
git fetch origin
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I thought a fetch would save a lot of bandwidth because both branches have a lot in common. The initial cloning took me about 10 minutes. – lalebarde Sep 22 '14 at 08:39
  • @lalebarde true, I have edited the answer, for you to test the fetch first. – VonC Sep 22 '14 at 08:42
  • Thanks VonC. When I try `git remote add -t branch2 origin remote-url`, I get `fatal: remote origin already exists.` – lalebarde Sep 22 '14 at 08:46
  • 1
    @lalebarde try deleting the remote first: `git remote remove origin` – VonC Sep 22 '14 at 08:47
  • `error: Unknown subcommand: remove`, but rm works. strange !? – lalebarde Sep 22 '14 at 08:49
  • @lalebarde what git version are you using? – VonC Sep 22 '14 at 08:49
  • 1
    @lalebarde that explains a lot. Either upgrade, or simply replace br1 with br2 in your git config. – VonC Sep 22 '14 at 08:52
  • It is already upgraded, but I am on a debian 7. I am new to debian. I assume it is very conservative ? kernel is 2.6.32. I am on a VPS. Anyway, `git remote add -t br2 origin https://www.github.com/project/project` is ok, and `git fetch origin` also in 33 sec. – lalebarde Sep 22 '14 at 09:00
  • 1
    @lalebarde great! I have updated my answer. Note that you can upgrade git: http://stackoverflow.com/a/25089714/6309 – VonC Sep 22 '14 at 09:01