跟踪单个远程分支作为本地分支非常简单。

$ git checkout --track -b ${branch_name} origin/${branch_name}

将所有本地分支推到远程,根据需要创建新的远程分支也很容易。

$ git push --all origin

我想做相反的事情。如果我在一个源上有X个远程分支:

$ git branch -r 
branch1
branch2
branch3
.
.
.

我是否可以为所有这些远程分支创建本地跟踪分支,而不需要手动创建每个分支?可以这样说:

$ git checkout --track -b --all origin

我用谷歌搜索了一下,但到目前为止都是胡扯。


当前回答

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

其他回答

你可以很容易地编写脚本,但我不知道什么时候它才有价值。这些分支很快就会落后,你必须一直更新它们。

远程分支将自动保持最新,因此在您实际想要处理它的地方创建本地分支是最简单的。

使用bash:

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

学分:瓦尔·布兰特、埃利亚斯和雨果

git 1.9.1之前

注意:以下代码如果在git (>v1.9.1)的更高版本中使用,会导致 (错误)所有创建的分支跟踪主 (烦恼)所有创建的本地分支名称都以origin/作为前缀

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

更新分支,假设你的本地跟踪分支没有变化:

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

忽略模棱两可的refname警告,git似乎更喜欢本地分支。

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

这里的大多数答案都使git分支-r的输出解析过于复杂。您可以使用下面的for循环针对远程上的所有分支创建跟踪分支,如下所示。

例子

假设我有这些远程分支。

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

确认我们在本地没有跟踪master以外的任何东西:

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

你可以使用这一行来创建跟踪分支:

$ 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.

现在确认:

$ git branch
  development
  integration
* master
  production
  staging

删除它们:

$ 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).

如果你使用-vv开关到git分支,你可以确认:

$ 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 ....

for循环失效

循环基本上调用命令git分支-r,使用grep -vE "HEAD|master"过滤输出中的任何HEAD或master分支。为了只得到分支的名称减去源/子字符串,我们使用Bash的字符串操作${var#stringtoremove}。这将从变量$var中删除字符串"stringtoremove"。在本例中,我们从变量$i中删除字符串origin/。

注意:或者你可以使用git checkout -track…要做到这一点:

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

但我并不特别关心这个方法,因为它在执行签出时在分支之间切换。完成后,它将把您留在它创建的最后一个分支上。

参考文献

10.1. 操纵字符串-高级Bash脚本编写指南 3.5 Git分支-远程分支

以防您已经签出了一些分支并想要

从远程检查所有剩余的分支 确保所有本地分支跟踪远程分支

您可以使用以下兼容bash和zsh的脚本:

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