Track all remote git branches as local branches

前端 未结 15 2123
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 09:01

Tracking a single remote branch as a local branch is straightforward enough.

$ git checkout --track -b ${branch_name} origin/${branch_name}
<
相关标签:
15条回答
  • 2020-11-22 09:29

    In case you already have some branches checked out and want to

    • check out all remaining branches from the remote
    • make sure all local branches track the remote branches

    you can use the following bash- and zsh-compatible script:

    git branch -r | while read b; do if git branch | grep -q " ${b##*/}$"; then git branch --set-upstream ${b##*/} $b; else git branch --track ${b##*/} $b; fi; done
    
    0 讨论(0)
  • 2020-11-22 09:30

    The answer given by Otto is good, but all the created branches will have "origin/" as the start of the name. If you just want the last part (after the last /) to be your resulting branch names, use this:

    for remote in `git branch -r | grep -v /HEAD`; do git checkout --track $remote ; done
    

    It also has the benefit of not giving you any warnings about ambiguous refs.

    0 讨论(0)
  • 2020-11-22 09:31

    If you want to use powershell and your remote is called origin. Then this works.

    git fetch    
    git branch -r  | %{$_ -replace "  origin/"} | %{git branch --track $_ "origin/$_"}
    
    0 讨论(0)
  • 2020-11-22 09:31

    Here is my solution of BASH command referring by @tjmcewan:

    for remote in `git branch -r | grep -v /HEAD `; do git branch --track ${remote/"origin/"/""}; done
    

    My goal is to solve the problem that all the created branches will have "origin/" as the start of the name, because I tested that $remote variables are still include "origin/":

    for remote in `git branch -r | grep -v /HEAD`; do echo $remote ; done
    
    0 讨论(0)
  • 2020-11-22 09:32
    for i in `git branch -a | grep remote`; do git branch --track ${i#remotes/origin/} $i; done
    
    0 讨论(0)
  • 2020-11-22 09:34

    without any scripting (in an empty directory):

    $ git clone --bare repo_url .git
    $ git config core.bare false
    $ git checkout
    

    after that, all remote branches will be seen as local.


    original (in russian).

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