将一个 git 分支分成两个分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12355904/
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-10 14:34:05  来源:igfitidea点击:

Split a git branch into two branches?

gitsplitbranch

提问by Matt V.

I'm trying to help out a coworker who accidentally created one feature branch from another feature branch, rather than creating the second one from master. Here is essentially what we have now…

我试图帮助一位同事不小心从另一个功能分支创建了一个功能分支,而不是从 master 中创建第二个。这基本上就是我们现在所拥有的……

Master ---A---B---C
                   \
              Foo   E---F---F---H
                                 \
                            Bar   J---K---L---M

And here is what we'd like to have…

这就是我们想要的……

Master ---A---B---C
                  |\
             Foo  | E---F---F---H
                  |
             Bar  J---K---L---M

One way I thought of would be to create FooV2 and BarV2 branches, and cherry-pick the individual commits into the appropriate V2 branches. But I'm curious, is there a better way to handle this situation?

我想到的一种方法是创建 FooV2 和 BarV2 分支,然后将各个提交挑选到适当的 V2 分支中。但我很好奇,有没有更好的方法来处理这种情况?

采纳答案by Don Branson

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:

我尝试了我的建议,但它不起作用。knittl 的建议不起作用(在我的盒子上)。以下是对我有用的内容:

git rebase --onto master foo bar

回答by cdeszaq

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 rebasecommand!

幸运的是,Git 在rebase命令的选项中为我们提供了解决方案!

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)
  1. rebase- 改变某事的父母
  2. --onto- 这是告诉 git 使用这种替代变基语法的标志
  3. newParent- 这是您正在变基的分支将拥有的分支,因为它是父分支
  4. oldParent- 这是您正在变基的分支当前拥有的分支,因为它是父分支
  5. branchToMove- 这是您要移动的分支(变基)

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 oldParentis how you define where the branch you are moving (ie. branchToMove) starts.

关于“旧父分支”的部分可能有点令人困惑,但它真正在做的是说“在执行 rebase 时忽略我的旧父分支的更改”。这oldParent就是您定义要移动的分支(即branchToMove开始的位置的方式

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 endof the "branch chain".

请注意,这些命令的顺序很重要,因为我们需要将每个分支从“分支链”的末端拉出。

回答by knittl

You can rebase your Bar branch onto master:

您可以将 Bar 分支重新设置为 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.

如果某些补丁冲突,您必须手动解决它们(但您也必须在挑选时这样做)。警告:当历史已经发布时不要重新设置基准。