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

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

问题描述

将一个远程分支作为本地分支进行跟踪很简单。

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

$ b

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

  $ git push  - 全部来源

我想做相反的事。如果我在单一来源中拥有X个远程分支:

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



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

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

我已经使用了Google和RTM,但到目前为止已经出现了垃圾信息。

解决方案

使用bash:



git 1.9.1

 我在`git branch -a | grep remote | grep -v HEAD | grep -v master`; git branch --track $ {i#remotes / origin /} $ i;完成




信用点: Val Blant,elias ,和Hugo




在git 1.9.1之前


注意:
下面的代码,如果在后来的git版本中使用(> v1.9.1)会导致


  1. (bug)所有创建的分支来跟踪master

  2. (烦恼)所有创建的本地分支名称前缀为 origin /




 用于远程`git branch -r`;做git分支--track $ remote;完成

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

 用于远程`git branch -r`;做git checkout $ remote; git pull; done 

忽略不明确的refname警告,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天全站免登陆