将所有远程 git 分支作为本地分支进行跟踪 [英] Track all remote git branches as local branches

查看:39
本文介绍了将所有远程 git 分支作为本地分支进行跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将单个远程分支作为本地分支进行跟踪非常简单.

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

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

$ git push --all origin

我想反过来.如果我在一个来源有 X 个远程分支:

$ git branch -r分支 1分支 2分支 3...

我可以为所有这些远程分支创建本地跟踪分支而无需手动创建每个分支吗?像这样说:

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

我在谷歌上搜索过和 RTM,但到目前为止还没有找到.

解决方案

使用 bash:

在 git 1.9.1 之后

for i in `git branch -a |grep 远程 |grep -v 头 |grep -v master`;做 git branch --track ${i#remotes/origin/} $i;完毕

<块引用>

致谢: Val Blat、elias 和 Hugo

git 1.9.1 之前<块引用>

注意:以下代码如果在更高版本的 git (>v1.9.1) 中使用会导致

  1. (bug) 所有创建的分支来跟踪 master
  2. (烦恼)所有创建的本地分支名称都以origin/
  3. 为前缀

 for remote in `git branch -r `;做 git branch --track $remote;完毕

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

 for remote in `git branch -r `;做 git checkout $remote ;git拉;完毕

忽略不明确的引用名称警告,git 似乎更喜欢本地分支.

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:

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

解决方案

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.

这篇关于将所有远程 git 分支作为本地分支进行跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆