Track all remote git branches as local branches

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

    Using bash:

    after git 1.9.1
    for i in `git branch -a | grep remote | grep -v HEAD | grep -v master`; do git branch --track ${i#remotes/origin/} $i; done
    

    credits: Val Blant, elias, and Hugo

    before git 1.9.1

    Note: the following code if used in later versions of git (>v1.9.1) causes

    1. (bug) All created branches to track master
    2. (annoyance) All created local branch names to be prefixed with origin/
    for remote in `git branch -r `; do git branch --track $remote; done
    

    Update the branches, assuming there are no changes on your local tracking branches:

    for remote in `git branch -r `; do git checkout $remote ; git pull; done
    

    Ignore the ambiguous refname warnings, git seems to prefer the local branch as it should.

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

    From git 2.23 onwards:

    for branch in `git branch -r | grep origin/`; do git switch -t -C ${branch#origin/} $branch; git pull; done
    

    The -C flag for git switch creates or resets if it already exists.

    git switch documentation

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

    Most of the answers here are over complicating the parsing of the output of git branch -r. You can use the following for loop to create the tracking branches against all the branches on the remote like so.

    Example

    Say I have these remote branches.

    $ git branch -r
      origin/HEAD -> origin/master
      origin/development
      origin/integration
      origin/master
      origin/production
      origin/staging
    

    Confirm that we're not tracking anything other than master already, locally:

    $ git branch -l    # or using just git branch
    * master
    

    You can use this one liner to create the tracking branches:

    $ for i in $(git branch -r | grep -vE "HEAD|master"); do 
        git branch --track ${i#*/} $i; done
    Branch development set up to track remote branch development from origin.
    Branch integration set up to track remote branch integration from origin.
    Branch production set up to track remote branch production from origin.
    Branch staging set up to track remote branch staging from origin.
    

    Now confirm:

    $ git branch
      development
      integration
    * master
      production
      staging
    

    To delete them:

    $ git br -D production development integration staging 
    Deleted branch production (was xxxxx).
    Deleted branch development (was xxxxx).
    Deleted branch integration (was xxxxx).
    Deleted branch staging (was xxxxx).
    

    If you use the -vv switch to git branch you can confirm:

    $ git br -vv
      development xxxxx [origin/development] commit log msg ....
      integration xxxxx [origin/integration] commit log msg ....
    * master      xxxxx [origin/master] commit log msg ....
      production  xxxxx [origin/production] commit log msg ....
      staging     xxxxx [origin/staging] commit log msg ....
    

    Breakdown of for loop

    The loop basically calls the command git branch -r, filtering out any HEAD or master branches in the output using grep -vE "HEAD|master". To get the names of just the branches minus the origin/ substring we use Bash's string manipulation ${var#stringtoremove}. This will remove the string, "stringtoremove" from the variable $var. In our case we're removing the string origin/ from the variable $i.

    NOTE: Alternatively you can use git checkout --track ... to do this as well:

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

    But I don't particularly care for this method, since it's switching you among the branches as it performs a checkout. When done it'll leave you on the last branch that it created.

    References

    • 10.1. Manipulating Strings - Advanced Bash Scripting Guide
    • 3.5 Git Branching - Remote Branches
    0 讨论(0)
提交回复
热议问题