如何重新合并已经合并的分支? [英] How to re-merge an already merged branch?

查看:632
本文介绍了如何重新合并已经合并的分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个提交图,看起来像下面的图.标记为*的提交代表很多提交.

I have a commit graph that looks something like the one below. Commits marked with * represent lots of commits.

   A*
   |
   B---------
   |        |
   C*       D*    <- old feature branch
   |        |
   E---------
   |
   F*
   |
   G      <- master

合并提交E执行不正确,并且C *中的某些更改(不是全部)已丢失.如何重做合并以将更改重新引入当前的主服务器?

The merge commit E was done incorrectly and some changes (not all) in C* have been lost. How can I redo that merge to reintroduce the changes into the current master?

所有内容都已推送(开源项目),因此无法更改历史记录.

Everything has already been pushed (open source project), so changing history is not an option.

我尝试从提交C *创建补丁并将其应用到主服务器,但由于<*>一些对C *的更改已正确合并,并且由于该提交以来项目已经发展,所以80%的补丁程序失败.

I tried creating a patch from the commits C* and apply that to the master, but because some of the changes from C* have been merged correctly and because the project evolved since that commit, about 80% of the patch fails.

理想情况下,我们将对C *进行所有更改,将其应用于主版本并解决所有冲突.但是因为分支已经合并,所以git无法检测到任何更改,也不允许再次合并.

Ideally, we would take all the changes in C*, apply them to master and resolve all conflicts. But because the branches have already been merged, git doesn't detect any changes and won't allow to merge again.

$ git checkout 5bc5295   # C
HEAD is now at 5bc5295... cleanup

$ git checkout -b "missing-commits"
Switched to a new branch 'missing-commits'

$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

$ git merge missing-commits
Already up-to-date.

推荐答案

理想情况下,我们将对C *进行所有更改,将其应用于主版本并解决所有冲突.

Ideally, we would take all the changes in C*, apply them to master and resolve all conflicts.

不,理想情况下,您可以倒带时间,以当时的方式进行合并(但这次是正确的),然后重新应用F*更改并以正确的master结尾.

No, ideally you would rewind time, do the merge as it was back then (but correctly, this time), then reapply the F* changes and end up with a correct master.

您可以这样做:

git checkout missing-commits
git checkout -b correct-merge
git merge D    # do it right, this time around!
git checkout master
git checkout -b correct-master
git rebase --onto correct-merge wrong-merge correct-master # have fun with the mother of all rebases!

如果您在重新设置基准站期间能够处理所有冲突,那么最终您将在分支correct-master中得到最初想要的结果.

If you manage to handle all the conflicts during the rebase, you will end up with exactly what you wanted, originally, in the branch correct-master.

由于您不想更改历史记录,因此可以创建一个大补丁并将其应用于当前的master,但是我更喜欢这种方法(假设您在工作目录yourrepos中做了所有工作) :

Since you do not want to change history, you could then create a big patch and apply that to your current master, but I like this approach more (assuming you did all your work in your working directory yourrepos):

cd yourrepos ; git checkout correct-master ; cd ..
cp -a yourrepos newrepos
rm -rf newrepos/.git
cd yourrepos ; git checkout master ; cd ..
cp -a yourrepos/.git newrepos/

现在,当您输入newrepos并执行git status时,您将位于分支master上,并且将精确地看到mastercorrect-master之间的所有更改,就像您已应用了修补程序一样.它将捕获已删除的文件,新文件,更改的文件,更改的权限,更改的符号链接等.

Now, when you enter newrepos and do a git status, you will be on branch master and will see precisely all changes between master and correct-master, as if you had applied a patch. It will catch deleted files, new files, changed files, changed permissions, changed symlinks and so on.

理想情况下,如果一切正常,它将准确显示C中缺少的提交.使用您最喜欢的git add变体,最后使用一个不错的git commit(或多个,如果您愿意),就可以了.没有任何历史记录被重写.

Ideally, if everything went right, it will show exactly the missing commits from C. Use your favourite variant of git add and finally a nice git commit (or several, if you prefer), and you're done. No history has been rewritten.

这篇关于如何重新合并已经合并的分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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