Split a git branch into two branches?

不羁岁月 提交于 2019-11-28 16:12:41

It looks to me like you could:

git checkout J
git rebase master

Edit:

I tried what I suggested and it doesn't work. knittl's suggestion doesn't work (on my box). Here's what did work for me:

git rebase --onto master foo bar

For a more general answer that will help us understand things a bit better than just "run this command", we need a larger example. So, let's pretend you're actually in this situation:

---A---B---C <= Master
            \
             E---F---F---H <= Foo
                          \
                           J---K---L---M <= Bar
                                        \
                                         N---O---P---Q <= Baz

And here is what we'd like to have…

---A---B---C <= Master
           |\
           | E---F---F---H <= Foo
           |\
           | J---K---L---M <= Bar
            \
             N---O---P---Q <= Baz

Thankfully, Git has a solution for us in the options to the rebase command!

git rebase --onto [newParent] [oldParent] [branchToMove]

What this means can be broken down into parts:

  1. rebase - Change the parents of something
  2. --onto - This is the flag that tells git to use this alternate rebase syntax
  3. newParent - This is the branch that the branch you are rebasing will have as it's parent
  4. oldParent - This is the branch that the branch you are rebasing currently has as it's parent
  5. branchToMove - This is the branch that you are moving (rebasing)

The bit about "old parent branch" can be a little confusing, but what it's really doing is it's saying "ignore the changes from my old parent when doing the rebase". The oldParent is how you define where the branch you are moving (ie. branchToMove) starts.

So, what are the commands to execute in our situation?

git rebase --onto Master Bar Baz
git rebase --onto Master Foo Bar

Note that the order of these commands matter because we need to pull each branch off of the end of the "branch chain".

You can rebase your Bar branch onto master:

git rebase --onto C H M

If some patches conflict, you have to resolve them manually (but you also have to do that when cherry picking). Word of caution: don't rebase when the history has been published already.

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