我如何将分支的一部分重新分配给主分支? [英] How can I rebase part of a branch to the master branch?
问题描述
C --- D(branch1)
/
A --- B(master)
因为我需要处理其他的东西与 branch1
>无关),我决定创建一个新分支(称为 branch2
)。我在 branch2
上做了几个提交,然后推送它们。
现在,我的意图是让 master
作为 branch2
的根。然而,我不小心做到了这一点:
E --- F - G(branch2)
/
C --- D(branch1)
/
A --- B(主)
有没有一种干净的方法可以直接从 master
中创建 branch2
>,如下所示?
E --- F - G(branch2)
|
| C --- D(branch1)
| /
A --- B(主)
git-rebase
手册页:
另一个
- 到
选项的例子是rebase分支的一部分。如果
我们有以下情况:
H --- I --- J topicB
/
E --- F --- G topicA
/
A --- B --- C --- D master
然后命令
git rebase --onto master topicA topicB
会导致:
H' - I' - J'topicB
/
| E --- F --- G topicA
| /
A --- B --- C --- D master
该示例表明您将通过运行
git rebase --onto master branch1 branch2
然而,当你的repo看起来像你的第二个ASCII图时,你还写道你已经将 branch2
推送到了某个远程。您是否与其他人共享遥控器?在这种情况下,在重新印制之前请多考虑一次(或三次!),因为这是历史重写的一种形式,并且你永远不应该重写历史记录。
Originally, I had something like this:
C---D (branch1)
/
A---B (master)
Because I needed to work on something else (which was unrelated to branch1
), I decided to make a new branch (called branch2
). I made a couple of commits on branch2
and then pushed them.
Now, my intention was to have master
as a root of branch2
. However, I accidentally did this:
E---F--G (branch2)
/
C---D (branch1)
/
A---B (master)
Is there a clean way to make branch2
stem directly from master
, as shown below?
E---F--G (branch2)
|
| C---D (branch1)
|/
A---B (master)
The ASCII graphs in your question have an uncanny resemblance to those in the second example describing the usage of the --onto
flag in the git-rebase
man page:
Another example of
--onto
option is to rebase part of a branch. If we have the following situation:H---I---J topicB / E---F---G topicA / A---B---C---D master
then the command
git rebase --onto master topicA topicB
would result in:
H'--I'--J' topicB / | E---F---G topicA |/ A---B---C---D master
That example indicates that you will land in the desired situation by running
git rebase --onto master branch1 branch2
However, you also write that you've already pushed branch2
to some remote when your repo looked like your second ASCII graph. Are you sharing the remote in question with anybody else? In that case, think twice (or thrice!) before rebasing, as it's a form of history rewriting, and you should never rewrite history has been shared.
这篇关于我如何将分支的一部分重新分配给主分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!