How to clone all remote branches in Git?

后端 未结 30 2077
情话喂你
情话喂你 2020-11-22 01:08

I have a master and a development branch, both pushed to GitHub. I\'ve cloned, pulled, and fetched, but I re

相关标签:
30条回答
  • 2020-11-22 01:42

    OK, when you clone your repo, you have all branches there...

    If you just do git branch, they are kind of hidden...

    So if you'd like to see all branches name, just simply add --all flag like this:

    git branch --all or git branch -a

    If you just checkout to the branch, you get all you need.

    But how about if the branch created by someone else after you clone?

    In this case, just do:

    git fetch

    and check all branches again...

    If you like to fetch and checkout at the same time, you can do:

    git fetch && git checkout your_branch_name

    Also created the image below for you to simplify what I said:

    0 讨论(0)
  • 2020-11-22 01:42

    The accepted answer of git branch -a only shows the remote branches. If you attempt to checkout the branches you'll be unable to unless you still have network access to the origin server.


    If you’re looking for a self-contained clone or backup that includes all remote branches and commit logs, use:

    git clone http://user@repo.url
    git pull --all
    

    Credit: Gabe Kopley's for suggesting using git pull --all.

    Note:
    Of course, if you no longer have network access to the remote/origin server, remote/origin branches will not have any updates reflected in them. Their revisions will reflect commits from the date and time you performed the 2 commands above.


    Checkout a *local* branch in the usual way with `git checkout remote/origin/` Use `git branch -a` to reveal the remote branches saved within your `clone` repository.

    To checkout ALL your clone branches to local branches with one command, use one of the bash commands below:

    $ for i in $(git branch -a |grep 'remotes' | awk -F/ '{print $3}' \ 
    | grep -v 'HEAD ->');do git checkout -b $i --track origin/$i; done
    

    OR

    If your repo has nested branches then this command will take that into account:

    for i in $(git branch -a |grep 'remotes' |grep -v 'HEAD ->');do \
    basename ${i##\./} | xargs -I {} git checkout -b {} --track origin/{}; done
    

    The above commands will `checkout` a local branch into your local git repository, named the same as the *`remote/origin/`* and set it to `--track` changes from the remote branch on the *`remote/origin`* server should you regain network access to your origin repo server once more and perform a `git pull` command in the usual way.
    0 讨论(0)
  • 2020-11-22 01:43

    A git clone is supposed to copy the entire repository. Try cloning it, and then run git branch -a. It should list all the branches. If then you want to switch to branch "foo" instead of "master", use git checkout foo.

    0 讨论(0)
  • 2020-11-22 01:43

    For copy-paste into command line:

    git checkout master ; remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'`; do git branch -D $brname ; git checkout -b $brname $remote/$brname ; done ; git checkout master
    

    For more readibility:

    git checkout master ;
    remote=origin ;
    for brname in `
        git branch -r | grep $remote | grep -v master | grep -v HEAD 
        | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'
    `; do
        git branch -D $brname ;
        git checkout -b $brname $remote/$brname ;
    done ;
    git checkout master
    


    This will:

    1. check out master (so that we can delete branch we are on)
    2. select remote to checkout (change it to whatever remote you have)
    3. loop through all branches of the remote except master and HEAD
      1. delete local branch (so that we can check out force-updated branches)
      2. check out branch from the remote
    4. check out master (for the sake of it)

    Based on answer of VonC.

    0 讨论(0)
  • 2020-11-22 01:44

    Here is another short one-liner command which creates local branches for all remote branches:

    (git branch -r | sed -n '/->/!s#^  origin/##p' && echo master) | xargs -L1 git checkout
    

    It works also properly if tracking local branches are already created. You can call it after the first git clone or any time later.

    If you do not need to have master branch checked out after cloning, use

    git branch -r | sed -n '/->/!s#^  origin/##p'| xargs -L1 git checkout
    
    0 讨论(0)
  • 2020-11-22 01:45

    This isn't too much complicated, very simple and straight forward steps are as follows;

    git fetch origin This will bring all the remote branches to your local.

    git branch -a This will show you all the remote branches.

    git checkout --track origin/<branch you want to checkout>

    Verify whether you are in the desired branch by the following command;

    git branch
    

    The output will like this;

    *your current branch 
    some branch2
    some branch3 
    

    Notice the * sign that denotes the current branch.

    0 讨论(0)
提交回复
热议问题