如何在GIT中将一个分支合并到另一个分支?
让我详细解释一下这个问题。
我有一个主 git 分支,在上面创建了一个新的侧分支 bug10101010,现在我不想将 bug10101010 合并到主分支。到目前为止一切都很好。 现在我有同一产品的不同分支,名为legacy。我不想将 bug10101010 合并到 GIT 中的旧分支。
有什么想法吗?
我不能直接合并它,因为分支 bug10101010 是从主分支中分离出来的,在遗留版本中我只需要分支 bug10101010 与其父分支之间的差异。
Let me explain the problem in detail.
I have a main git branch on which I created a new side branch bug10101010, now I wan't to merge the bug10101010 to main. So far everything is good.
Now I have a different branch of the same product, named legacy. I wan't to merge the bug10101010 to the legacy branch in GIT.
Any ideas?
I can't just merge it directly, as the branch bug10101010 is spin off from the main branch and in the legacy I need only the diff between the branch bug10101010 and its parent branch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该在此处使用
git rebase --onto
并指定一个范围。(请参阅
git rebase
手册页:)。
当然,这会将您的
bug10
分支移动到legacy
分支之上,这不是您想要/需要的。因此,一种解决方法是在克隆存储库中执行变基操作,然后合并该“增强”
遗留
分支(克隆存储库中的分支,与bug10
修改)到本地和当前的legacy
分支(您要修改的分支,同时保留bug10
独自的)。现在:
legacy
分支,所以其他答案(补丁)是有效(并且更简单)。legacy
之前重新设置您想要的内容(例如bug10
提交)到您的原始存储库(您不会推送bug10
,因为它的历史记录将被完全重写!)我只是想看看它是否有效,所以...让我们测试一下该方法。
(Git1.6.5.1,在旧的 XP SP2 上,具有 Powershell 1.0 会话,因为
Start-Transcript
命令)我喜欢我不再需要先创建 git repo 目录,然后在其中输入
git init
! 自 1.6.5 起:这太棒了!
让我们创建 3 个文件,用于 3 个不同的目的。
(为了举例,我将在每个分支中单独保留文件修改:在此处合并或变基期间不会发生冲突。)
此时, git log --graph --oneline --branches 返回:
让我们构建一个
legacy
分支我们返回master,进行另一个提交,我们将标记为“2.0”(一个需要修复一些错误的版本!)
我们有:
现在我们做一个
bug10
错误修复分支:让我们在主分支上添加最终提交
主存储库的最终状态:
在这个阶段,我不会在 mainRepo 中进行任何进一步的操作。我只会克隆它来进行一些测试。如果这些失败,我可以随时返回此存储库并再次克隆它。
第一个克隆实际上是强制性的,为了执行我们的 git rebase --onto ,
我们需要克隆的存储库中的两个 mainRepo 分支:
让我们仅对 bug10 进行变基(即 < 之后的所有提交) code>2.0 标签一直到
bug10
分支的HEAD
):此时
bug10
已在顶部重放遗留
没有所有其他中间提交。现在,我们可以将
legacy
的HEAD
快进到重播的bug10
分支的顶部。内容符合我们的需要:
:
main
分支的内容仅存在于1.0
标记(legacy
分支的根),并且不再有强>。bug10
修复如下:就是这样。
我们的想法是在原始存储库中提取“增强”
legacy
分支,该分支的bug10
仍保持不变(即仍从2.0
开始) code> 标记,并且不会像我们在rebaseRepo
上那样在任何地方重播。在此克隆的存储库中,我跟踪
origin/legacy
分支,以便在其上合并另一个远程源的legacy
分支:<代码>rebaseRepo。在这个原始存储库中(我克隆它只是为了不弄乱 mainRepo 的状态,以防我还有其他一些实验要做),我将声明
rebaseRepo
为远程,并获取其分支。我们现在可以更新
legacy
,而无需触及bug10
:只要需要提交新的
bug10
,您就可以根据需要多次重复该过程。在旧的legacy
分支之上重放,不包括所有中间提交。You should use
git rebase --onto
here, and specify a range.(see
git rebase
man page:).
Of course, this would move your
bug10
branch on top of thelegacy
branch, which is not what you want/need.So, one workaround would be to do that rebase in a cloned repo, then merge that 'enhanced'
legacy
branch (the one in the clone repo, with thebug10
modifications on top of it) to the local and currentlegacy
branch (the one you want to modify, while leavingbug10
alone).Now:
legacy
branch, so the other answers (patch) are valid (and simpler).bug10
commits), before pushing only that branchlegacy
to your original repo (you would not pushbug10
, since its history would have been entirely rewritten!)I just wanted to see if it works, so... Let's test that approach.
(Git1.6.5.1, on a old XP SP2, with a Powershell 1.0 session because of the
Start-Transcript
command)I like how I do not have anymore to make the git repo directory first, then type in it
git init
! Since 1.6.5:This is GREAT!
Let's create 3 files, for 3 different purposes.
(For the sake of example, I will keep the file modifications separate per branch: no conflict during merge or rebase here.)
At this point, a
git log --graph --oneline --branches
returns:Let's build a
legacy
branchWe return to master, make another commit, which we will tag "2.0" (a release which will need some bug-fixing!)
We have:
Now we do a
bug10
bug-fixing branch:Let's add a final commit on the main branch
Final state of our main repo:
At this stage, I will not make any further manipulation in mainRepo. I will only clone it to make some tests. If those fails, I can always get back to this repo and clone it again.
The first clone is actually mandatory, in order to perform our
git rebase --onto
We need two of the mainRepo branches in our cloned repo:
Let's rebase only bug10 (that is all commits after
2.0
tag up toHEAD
ofbug10
branch):At this point
bug10
has been replayed on top oflegacy
without all the other intermediate commits.We can now fast-forward
HEAD
oflegacy
to the top of the replayedbug10
branch.The content follow what we need:
main
branch is there only up to1.0
tag (root forlegacy
branch), and not any further.bug10
fixes are here:That's it.
The idea is to to pull that 'enhanced'
legacy
branch in your original repo, which will still have itsbug10
unchanged (i.e. still starting from the2.0
tag, and not replayed anywhere like we did on therebaseRepo
.In this cloned repo, I track the
origin/legacy
branch, in order to merge on it thelegacy
branch of another remote source: therebaseRepo
.In this original repo (I only cloned it to not mess with the state of the mainRepo, in case I had some other experiments to do), I will declare
rebaseRepo
as a remote, and fetch its branches.We can now update
legacy
without touching tobug10
:You can repeat the process as many time as you want, whenever new
bug10
commits need to be replayed on top of an oldlegacy
branch, without including all the intermediate commits.这很难做到。 Git 保存合并历史记录,如果你“cherrypick”并指向 bug10101010 中的提交作为父项(表明你已经完成了合并),Git 会假设之前的所有提交(回到它们拆分的点)都已合并为出色地。当您想要进行“真正的”合并时会给您带来问题。
另一方面,您可以从该(且仅该)特定提交手动生成补丁。但是,当您稍后进行“真正的”合并时,这也会给您带来问题,因为它会尝试应用您手动处理的提交两次。
但话又说回来,由于一个分支被命名为“Legacy”,我怀疑您无论如何都不打算进行真正的合并,在这种情况下,您几乎可以自由地按照自己的意愿进行合并。
这是关于该主题的有趣博客文章。
This is hard to do. Git saves merge history, and if you "cherrypick" and point at a commit in bug10101010 as a parent (indicating you have done a merge) Git will assume that all commits before that (back to the point where they split) as been merged as well. Giving you problems when you want to do a "real" merge.
On the other hand you can just manually generate a patch from that (and only that) specific commit. But that will also give you problems when you later do the "real" merge, since it tries to apply your manually handled commit twice.
But then again, since one branch is named "Legacy" I suspect that you dont plan to do that real merge anyway, in which case youre pretty much free to do it anyway you please.
Heres an interesting blog post on the topic.
使用 git-diff 然后 git-apply?
Use git-diff and then git-apply?