Git:如何redirect到特定的提交?

我想重定义一个具体的提交,而不是另一个分支的头:

A --- B --- C master \ \-- D topic 

 A --- B --- C master \ \-- D topic 

代替

 A --- B --- C master \ \-- D topic 

我怎样才能做到这一点?

你可以通过在你喜欢的提交上创build一个临时分支来避免使用–onto参数,然后以简单的forms使用rebase:

 git branch temp master^ git checkout topic git rebase temp git branch -d temp 

你甚至可以采取直接的方法:

 git checkout topic git rebase <commitB> 

使用“到”选项:

 git rebase --onto master^ D^ D 

我已经使用了上述的一系列解决scheme:

 $ git branch temp <specific sha1> $ git rebase --onto temp master topic $ git branch -d temp 

我发现阅读和理解起来要容易得多。 被接受的解决scheme导致我合并冲突(懒得手工修复):

 $ git rebase temp First, rewinding head to replay your work on top of it... Applying: <git comment> Using index info to reconstruct a base tree... M pom.xml .git/rebase-apply/patch:10: trailing whitespace. <some code> .git/rebase-apply/patch:17: trailing whitespace. <some other code> warning: 2 lines add whitespace errors. Falling back to patching base and 3-way merge... Auto-merging pom.xml CONFLICT (content): Merge conflict in pom.xml error: Failed to merge in the changes. Patch failed at 0001 <git comment> The copy of the patch that failed is found in: .git/rebase-apply/patch When you have resolved this problem, run "git rebase --continue". If you prefer to skip this patch, run "git rebase --skip" instead. To check out the original branch and stop rebasing, run "git rebase --abort". 

上面的jsz的评论节省了我很多的痛苦,所以我一直使用下面的algorithm来重新绑定/移动任何提交之上,因为我发现它:

  1. find分支的前一个分支点(移动) – 称之为老的父母。 在上面的例子中是A.
  2. find您想要将分支移到其上的提交 – 将其称为新父项。 在B的考试中
  3. 你需要在你的分支(你移动的那个)上:
  4. 应用你的rebase: git rebase --onto <new parent> <old parent>

在上面的例子中,这很简单:

  git checkout topic git rebase --onto BA