简体   繁体   English

将所有远程 git 分支作为本地分支进行跟踪

[英]Track all remote git branches as local branches

Tracking a single remote branch as a local branch is straightforward enough.将单个远程分支作为本地分支进行跟踪非常简单。

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

Pushing all local branches up to the remote, creating new remote branches as needed is also easy.将所有本地分支推送到远程,根据需要创建新的远程分支也很容易。

$ git push --all origin

I want to do the reverse.我想做相反的事情。 If I have X number of remote branches at a single source:如果我在一个来源有 X 个远程分支:

$ git branch -r 
branch1
branch2
branch3
.
.
.

Can I create local tracking branches for all those remote branches without needed to manually create each one?我可以为所有这些远程分支创建本地跟踪分支而无需手动创建每个分支吗? Say something like:像这样说:

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

I've googled and RTMs, but have come up bunk thus far.我已经用谷歌搜索过和 RTM,但到目前为止还没有找到。

The answer given by Otto is good, but all the created branches will have "origin/" as the start of the name. Otto 给出的答案很好,但是所有创建的分支都将以“origin/”作为名称的开头。 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.它还具有不给您任何关于不明确引用的警告的好处。

Using bash:使用 bash:

after git 1.9.1 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 在 git 1.9.1 之前

Note: the following code if used in later versions of git (>v1.9.1) causes注意:以下代码如果在更高版本的 git (>v1.9.1) 中使用会导致

  1. (bug) All created branches to track master (错误)所有创建的分支来跟踪主
  2. (annoyance) All created local branch names to be prefixed with origin/ (烦恼)所有创建的本地分支名称都以origin/为前缀
for remote in `git branch -r `; do git checkout $remote ; git pull; 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.忽略模棱两可的 refname 警告,git 似乎更喜欢本地分支,因为它应该。

Most of the answers here are over complicating the parsing of the output of git branch -r .这里的大多数答案都使git branch -r的输出解析过于复杂。 You can use the following for loop to create the tracking branches against all the branches on the remote like so.您可以使用以下for循环针对远程上的所有分支创建跟踪分支,如下所示。

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:确认我们没有在本地跟踪除 master 以外的任何内容:

$ 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:如果您使用-vv切换到git branch您可以确认:

$ 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 for循环分解

The loop basically calls the command git branch -r , filtering out any HEAD or master branches in the output using grep -vE "HEAD|master" .该循环基本上调用命令git branch -r ,使用grep -vE "HEAD|master"过滤掉输出中的任何 HEAD 或 master 分支。 To get the names of just the branches minus the origin/ substring we use Bash's string manipulation ${var#stringtoremove} .为了仅获取分支的名称减去origin/子字符串,我们使用 Bash 的字符串操作${var#stringtoremove} This will remove the string, "stringtoremove" from the variable $var .这将从变量$var删除字符串“stringtoremove”。 In our case we're removing the string origin/ from the variable $i .在我们的例子中,我们从变量$i删除了字符串origin/

NOTE: Alternatively you can use git checkout --track ... to do this as well:注意:或者,您也可以使用git checkout --track ...来执行此操作:

$ 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参考文献

Update Q1 2020: Mohsen Abasi proposes in the comments , based on the 2014 slm 's answer , the simpler alternative: 2020 年第一季度更新: Mohsen Abasi 在评论中提出,基于 2014 slm回答,更简单的替代方案:

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 .正如我在另一个旧答案中提到的,使用git for-each-ref可能更快
And I would use the new (Git 2.23+) git switch command , which replaces the confusing git checkout .我会使用新的 (Git 2.23+) git switch command ,它取代了令人困惑的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.这样,就不需要grep


Old (2011) original answer:旧(2011)原始答案:

Here is my one-liner I use (in a bash shell, tested with msysgit1.7.4):这是我使用的单行(在 bash shell 中,使用 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).它只会从您在remote变量中指定的remote选择上游分支(它可以是“ origin ”或您为当前 Git 存储库的远程之一设置的任何名称)。
  • it will extract the name of the branch: origin/a/Branch/Name => a/Branch/Name through the awk expression.它将通过awk表达式提取分支的名称: origin/a/Branch/Name => a/Branch/Name
  • it will set the upstream branch through --set-upstream-to (or -u ) , not --track :它将通过--set-upstream-to (或-u而不是--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.xxx.(remote|merge)设置。

     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.该命令将为所有远程上游分支创建本地分支,并将其远程和合并设置设置为该远程分支。

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.远程分支会自动保持最新状态,因此最简单的方法是在您真正想要处理的地方创建本地分支。

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) .原文(俄文)

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

If you want to use powershell and your remote is called origin.如果您想使用 powershell 并且您的遥控器称为 origin。 Then this works.然后这有效。

git fetch    
git branch -r  | %{$_ -replace "  origin/"} | %{git branch --track $_ "origin/$_"}
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do  git branch --track ${branch##*/} $branch; done

使用这个,你不会有这样的警告: refname 'origin/dev' is ambiguous

Here is my solution of BASH command referring by @tjmcewan:这是@tjmcewan引用的BASH命令的解决方案:

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/":我的目标是解决所有创建的分支将以“origin/”作为名称开头的问题,因为我测试了$remote变量仍然包含“origin/”:

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

To do the same as tjmcewan's answer but on Windows, call this from a batch file :要执行与tjmcewan 的回答相同但在 Windows 上的操作,请从批处理文件中调用:

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

From git 2.23 onwards:从 git 2.23 开始:

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-C标志创建或重置,如果它已经存在。

git switch documentation git 开关文档

VonC's solution can be simplified even further by changing the sed (I lack the rep points to comment directly on his post): VonC 的解决方案可以通过更改 sed 进一步简化(我没有直接评论他的帖子的代表点):

for branch in $(git branch -r | sed 's,[^/]*/,,g'); do git switch $branch; done

By replacing everything that isn't a slash up to the final slash, the remaining branch name is suitable for local use;通过将不是斜线的所有内容替换到最后的斜线,剩余的分支名称适合本地使用; repeatedly switching to the same branch isn't an error (it may be inefficient, but it may be more efficient than having a grep in the pipe :->).重复切换到同一个分支不是错误(它可能效率低下,但它可能比在管道中使用 grep 更有效:->)。

The switch command is smart enough to track each remote branch as necessary. switch 命令足够智能,可以根据需要跟踪每个远程分支。

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:您可以使用以下与 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
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;第 1 行:'git branch -r'(后跟 'git remote update' 以更新有关远程更改的信息)列出所有远程分支; 'egrep -vw' is used to knock entries having HEAD and master in the result. 'egrep -vw' 用于敲除结果中包含 HEAD 和 master 的条目。

line 3: Track the named remote branch while checking it out locally.第 3 行:在本地签出时跟踪指定的远程分支。 A simple awk is used to avoid 'origin/' being the suffix for local branches.一个简单的 awk 用于避免“origin/”成为本地分支的后缀。

Using bash, If you want to checkout all branches:使用 bash,如果要结帐所有分支:

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.重要的是要注意,当您执行导致新远程跟踪分支关闭的提取时,您不会自动拥有它们的本地、可编辑副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM