How to clone all remote branches in Git?

后端 未结 30 2161
情话喂你
情话喂你 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: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.

提交回复
热议问题