Git:如何合并多年来分歧很大的复杂分支 [英] Git: How to merge complex branches that have diverged a lot over the years

查看:118
本文介绍了Git:如何合并多年来分歧很大的复杂分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前已经将一个巨大的项目从 SVN 导入到 Git.我决定展平所有分支,只保留 2 个.Master 分支和 Diverged 分支.

I currently have imported a huge project from SVN to Git. I decided to flatten out all branches and keep only 2. Master branch and Diverged branch.

Master: A-B-C-D
               \ X-Y: Bugfix

Diverged: E-F-G-H

diverged 包含很多不同的源代码,但有些部分仍然来自 master.当我们必须做一些错误修正时,我们必须手动修补 SVN 上的 Master 和 Diverged.我想将Bugfix合并到master中(这里没问题)并将Bugfix合并到Diverged中.

The diverged contains a lot of different source code, but some parts still originate from master. When we have to do some bugfixing we have to manually patch Master and Diverged on SVN. I would like to merge Bugfix into master (no problem here) and merge Bugfix into Diverged.

请记住,我不希望将先前的提交插入到与 master 的 Diverged 中.所以ABCD应该被忽略.我正在寻找的结构是这样的:

Keep in mind that i dont want the previous commits to be inserted into Diverged from master. So ABCD should be ignored. The structure im looking for is this:

Master: A-B-C-D-X-Y

Diverged: E-F-G-H-X-Y

谁能帮我解决这个问题?

Could anyone help me figure this out?

推荐答案

合并不同的分支总是很痛苦.

Merging diverged branches is always painful.

首先,您必须记住一些有用的 git 命令,它们将帮助您进行怪物合并.

First of all you have to remember some useful git commands that will help you with a monster merge.

git log HEAD..origin/master - 将显示分支之间的差异.

git log HEAD..origin/master - will show you the differences between branches.

如果简单的 git merge origin/master 不起作用,请尝试将合并拆分为一系列较小的任务.我建议将您的分支分支重新建立在当前 master 的基础上,以便将更改本地化:

If simple git merge origin/master does not work try to split the merge into a series of smaller tasks. I suggest rebasing your diverged branch on top of the current master, so that the changes will be localized:

git rebase remotes/origin/master

您最终会面临解决每个冲突(可能在多次提交中)的无聊任务,但这些小任务比一次处理整个合并要简单得多.

You will end up with a boring task of resolving each and every conflict (possibly in many commits), but these small tasks are much simpler then dealing with the whole merge at once.

Master: A-B-C-D-X-Y
Diverged: E-F-G-H-X-Y

git checkout diverged
git rebase master

rebase 完成后,您可以将 master 快进到分支的顶端:

After rebasing is complete you can fast-forward your master to the tip of the branch:

Master: A-B-C-D-X-Y
                  \ E-F-G-H

空提交将被省略.在最复杂的场景中,您可以在分支上使用交互式 rebase 来重新排列提交.即,对于您的方案:

Empty commits will be omitted. In the most complex scenarios you can use interactive rebase on your branch to rearrange the commits. I.e, for your scheme:

虽然是体力活,但还是分而治之.

Though it is manual work but it is still a divide-and-conquer tactic.

阅读这篇文章以获得一些深入的想法:http://blog.springsource.org/2010/12/21/git-and-social-coding-how-to-merge-without-fear/

Read this post for some in-depth ideas: http://blog.springsource.org/2010/12/21/git-and-social-coding-how-to-merge-without-fear/

这篇关于Git:如何合并多年来分歧很大的复杂分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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