How to clone all remote branches in Git?

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

    Here's an answer that uses awk. This method should suffice if used on a new repo.

    git branch -r | awk -F/ '{ system("git checkout " $NF) }'
    

    Existing branches will simply be checked out, or declared as already in it, but filters can be added to avoid the conflicts.

    It can also be modified so it calls an explicit git checkout -b -t / command.

    This answer follows Nikos C.'s idea.


    Alternatively we can specify the remote branch instead. This is based on murphytalk's answer.

    git branch -r | awk '{ system("git checkout -t " $NF) }'
    

    It throws fatal error messages on conflicts but I see them harmless.


    Both commands can be aliased.

    Using nobody's answer as reference, we can have the following commands to create the aliases:

    git config --global alias.clone-branches '! git branch -r | awk -F/ "{ system(\"git checkout \" \$NF) }"'
    git config --global alias.clone-branches '! git branch -r | awk "{ system(\"git checkout -t \" \$NF) }"'
    

    Personally I'd use track-all or track-all-branches.

提交回复
热议问题