How to clone all remote branches in Git?

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

    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.

提交回复
热议问题