Search code examples
gitremote-branch

Using Git, I have cloned a branch and would like to update to another remote branch


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

Solution

  • 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