git 不小心从错误的分支分支出来,当我想合并到 master 时,我必须合并两个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21805925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:44:21  来源:igfitidea点击:

Accidentally branched off of the wrong branch, and when I want to merge into the master I have to merge both branches

gitversion-controlmerge

提问by Levi S

So I created a branch off of another branch I was already creating, and now when I try to merge the branches into master, I have run into the situation where I have to merge both branches.

所以我从我已经创建的另一个分支创建了一个分支,现在当我尝试将这些分支合并到 master 时,我遇到了必须合并两个分支的情况。

Here is a diagram

这是一个图表

Master->

 ->Branch 1  -> Branch 2

大师->

 ->Branch 1  -> Branch 2

I want to be able to merge just the changes on branch 2 onto master without having to merge the changes on Branch 1 if that makes sense. I looked into reset and revert, but it seems like these things will delete all the changes I made with branch 2. Any ideas?

如果有意义,我希望能够将分支 2 上的更改合并到 master 上,而不必合并分支 1 上的更改。我研究了重置和恢复,但似乎这些东西会删除我对分支 2 所做的所有更改。有什么想法吗?

Thanks

谢谢

回答by somesh

Try git rebase --onto with the following syntax:

尝试使用以下语法 git rebase --onto :

To put branch2's changes on to the master without including branch1's

将 branch2 的更改放在 master 上而不包括 branch1 的

git rebase --onto master branch1 branch2

Relevant output from git help rebase:

相关输出git help rebase

Here is how you would transplant a topic branch based on one branch to another, to pretend that you forked the topic branch from the latter branch, using rebase --onto.

First let's assume your topicis based on branch next. For example, a feature developed in topicdepends on some functionality which is found in next.

              o---o---o---o---o  master
                   \
                    o---o---o---o---o  next
                                     \
                                      o---o---o  topic

We want to make topicforked from branch master; for example, because the functionality on which topicdepends was merged into the more stable masterbranch. We want our tree to look like this:

              o---o---o---o---o  master
                  |            \
                  |             o'--o'--o'  topic
                   \
                    o---o---o---o---o  next

We can get this using the following command:

git rebase --onto master next topic

以下是将基于一个分支的主题分支移植到另一个分支的方法,以假装您使用rebase --onto.

首先让我们假设您的主题基于 branch next。例如,在topic 中开发的功能取决于在next 中找到的某些功能。

              o---o---o---o---o  master
                   \
                    o---o---o---o---o  next
                                     \
                                      o---o---o  topic

我们想让主题从分支master分叉出来;例如,因为主题所依赖的功能被合并到更稳定的分支中。我们希望我们的树看起来像这样:

              o---o---o---o---o  master
                  |            \
                  |             o'--o'--o'  topic
                   \
                    o---o---o---o---o  next

我们可以使用以下命令获得它:

git rebase --onto master next topic

Keep in mind the risks and pitfalls of rebasing, mentioned here: http://git-scm.com/book/en/Git-Branching-Rebasing

请记住这里提到的变基的风险和陷阱:http: //git-scm.com/book/en/Git-Branching-Rebasing

回答by user376507

One way to do it is, use cherry-pick. Do a git log branch2and find the commit id's you want and then switch to masterbranch using git checkout masterthen use git cherry-pick <commit_id>

一种方法是,使用cherry-pick. 做一个git log branch2并找到你想要的提交ID,然后master使用git checkout master然后使用切换到分支git cherry-pick <commit_id>

Refer to this post for more details

有关更多详细信息,请参阅此帖子

How to merge a specific commit in Git

如何在 Git 中合并特定提交