How can I rebase part of a branch to the master branch?

99封情书 提交于 2019-12-20 02:28:14

问题


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)

回答1:


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.



来源:https://stackoverflow.com/questions/28715619/how-can-i-rebase-part-of-a-branch-to-the-master-branch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!