Track all remote git branches as local branches

前端 未结 15 2125
佛祖请我去吃肉
佛祖请我去吃肉 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:35

    Update Q1 2020: Mohsen Abasi proposes in the comments, based on the 2014 slm's answer, the simpler alternative:

    for i in $(git branch -r | grep -vE "HEAD|master" | sed 's/^[ ]\+//'); 
    

    And it uses $() instead of obsolete backticks.

    As I mention in another old answer, using git for-each-ref is probably faster.
    And I would use the new (Git 2.23+) git switch command, which replaces the confusing git checkout.

    for i in $(git for-each-ref --format=%(refname:short) \
      --no-merged=origin/HEAD refs/remotes/origin); do \
        git switch --track $i; \
    done
    

    That way, no grep needed.


    Old (2011) original answer:

    Here is my one-liner I use (in a bash shell, tested with msysgit1.7.4):

    For copy-paste:

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

    For more readability:

    remote=origin ; // put here the name of the remote you want
    for brname in `
        git branch -r | grep $remote | grep -v master | grep -v HEAD 
        | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'
    `; do 
        git branch --set-upstream-to $remote/$brname $brname; 
    done
    
    • it will only select upstream branches from the remote you specify in the remote variable (it can be 'origin' or whatever name you have set for one of the remotes of your current Git repo).
    • it will extract the name of the branch: origin/a/Branch/Name => a/Branch/Name through the awk expression.
    • it will set the upstream branch through --set-upstream-to (or -u), not --track:
      The advantage is that, if the branch already exists, it won't fail and it won't change that branch origin, it will only configure the branch.xxx.(remote|merge) setting.

      branch.aBranchName.remote=origin
      branch.aBranchName.merge=refs/heads/a/Branch/Name
      

    That command will create local branches for all remote upstream branches, and set their remote and merge setting to that remote branch.

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

    Using bash, If you want to checkout all branches:

    for remote in `git branch -r`; do git checkout $(echo $remote | cut -d'/' -f 2); done
    

    It’s important to note that when you do a fetch that brings down new remote-tracking branches, you don’t automatically have local, editable copies of them.

    0 讨论(0)
  • 2020-11-22 09:38
    for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do  git branch --track ${branch##*/} $branch; done
    

    Use this and you will not have such warning as: refname 'origin/dev' is ambiguous

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

    To do the same as tjmcewan's answer but on Windows, call this from a batch file:

    for /f "delims=" %%r in ('git branch -r ^| grep -v master') do git checkout --track %%r
    

    Or this from the command line:

    for /f "delims=" %r in ('git branch -r ^| grep -v master') do git checkout --track %r
    
    0 讨论(0)
  • 2020-11-22 09:46
    for rembranch in `git remote update 2>&1 > /dev/null ; git branch -r|egrep -wv "HEAD|master"`
    do 
        git checkout --track -b `echo $rembranch|awk -F\/ '{print $2}'` $rembranch; 
    done
    

    Explanation:

    line 1: 'git branch -r' (followed by 'git remote update' to update the info on changes to remote) lists all remote branches; 'egrep -vw' is used to knock entries having HEAD and master in the result.

    line 3: Track the named remote branch while checking it out locally. A simple awk is used to avoid 'origin/' being the suffix for local branches.

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

    You could script that easily enough, but I don't know when it'd be valuable. Those branches would pretty quickly fall behind, and you'd have to update them all the time.

    The remote branches are automatically going to be kept up to date, so it's easiest just to create the local branch at the point where you actually want to work on it.

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