Git:如何一次重新绑定很多分支(使用相同的基本提交)? [英] Git: How to rebase many branches (with the same base commit) at once?

查看:108
本文介绍了Git:如何一次重新绑定很多分支(使用相同的基本提交)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中有一个主分支,用于从其他人处获取更改。从那以后,我通常有几个主题分支,我目前正在工作。



我的问题是:有没有办法让我把新的变化引入我的主人,然后将所有我的主题分支重新绑定到一次?



这是情况:

  D  -  E topic1 
/
A - B - C master
\
F - G topic2

我想用一个命令(H来自上游)完成此操作:

 
D' - E'topic1
/
A - B - C - H master
\
F' --G'topic2

现在,我知道我可以通过将主题1和主题2归入主人来完成此任务,甚至可以编写脚本自动执行此操作。但是如果我有其他几个分支,创建新分支并经常删除其他分支,并且我总是收到上游变更?

这个操作(几个rebase)手,既累人又容易出错。

有没有更简单的方法?



谢谢!

p>

解决方案

我确信没有办法自动执行此操作。请记住,git rebase master也可以让你回到需要你解决合并冲突的shell,所以如果你想写一个脚本来自动化所有这些,你需要考虑这一点。



不过,您可以很容易地跟踪哪些分支需要更新。嗯,对于任何分支,如果分支不是最新的wrt(即,只是承诺在master上),那么git rev-list branch..master将产生输出。所以你需要循环遍历除master以外的所有本地头,以产生一个报告(nbgit show-branch将大致这样做):

  git for-each-ref'refs / heads / *'| \ 
读取rev类型ref;做
branch = $(expr$ ref:'refs / heads / \(。* \)')
revs = $(git rev-list $ rev..master)
如果[-n$ revs];那么
echo $ branch需要更新
git diff --summary --shortstat -M -C -C $ rev master
fi
完成


当然,如果没有冲突,那么很容易......它产生的东西在不容易出现问题时也可以工作:p



如果你的某个分支是基于别的东西的话,这不一定会发生什么......这就是为什么我提到使用git pull --rebase因为这将根据分支配置进行重新分配,而不是由主人盲目分配。虽然检测不是基于分支配置......也许最简单的办法就是检查每个分支并执行git pull并让分支配置处理所有事情,包括是否进行分配或合并?


I have a master branch in my project, that I use to pull changes from other people. From that, I usually have several topic branches on which I'm currently working.

My question is: Is there a way for me to pull new changes into my master and then rebase ALL of my topic branches onto that at once?

This is the situation:

        D--E topic1
       /
A--B--C  master
       \
        F--G topic2

And I want to accomplish this with one single command (H came from upstream) :

               D'--E' topic1
              /
    A--B--C--H  master
              \
               F'--G' topic2

Now, I know I can accomplish this by rebasing topic1 and topic2 onto master, and I could even write a script to automate this. But what if I have several other branches, create new ones and delete others frequently and I receive upstream changes all the time?

This operation (several rebases), when done by hand, is both tiring and error-prone.

Is there an easier way?

Thanks!

解决方案

I'm fairly sure that there isn't a way to automatically do this. Remember that "git rebase master" can also drop you back to the shell needing you to resolve merge conflicts, so if you want to write a script to automate all this you need to take that into account.

You can fairly easily track which branches need updating, though. Hmm, for any branch, "git rev-list branch..master" will produce output if the branch is not up-to-date wrt (i.e. just commits on top of) master. So you need to loop through all the local heads except master to produce a report (nb "git show-branch" will approximately do this):

git for-each-ref 'refs/heads/*' | \
  while read rev type ref; do
    branch=$(expr "$ref" : 'refs/heads/\(.*\)' )
    revs=$(git rev-list $rev..master)
    if [ -n "$revs" ]; then
      echo $branch needs update
      git diff --summary --shortstat -M -C -C $rev master
    fi
  done

So if you were feeling brave, you could replace that "git diff" with something like "git checkout $branch && git rebase master" (or maybe just "git pull --rebase" if you've set that up). I think you'd then have to check for the existence of a ".git/rebase-apply" directory or check the index for unmerged files ("git ls-files -u") to test if we've been left waiting to do a merge.

Of course, if there are no conflicts, then it's easy... it's producing something that also works when it's not easy that's the problem :p

And this doesn't necessarily address what happens if one of your branches is based on something else... that's why I mentioned using "git pull --rebase" instead, because that would rebase according to the branch configuration, not blindly from master. Although the detection isn't based on the branch configuration... maybe it would be easiest just to check out each branch and do "git pull" and let the branch configuration handle everything, including whether to rebase or merge?

这篇关于Git:如何一次重新绑定很多分支(使用相同的基本提交)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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