git:如何将一些提交移动到新的分支

我一直在直线上工作:

A---B---C---D---E---F (master:HEAD) 

现在我想要退后一步:

 git checkout C 

并将最后几次提交移动到一个新的分支:

选项1:

  D---E---F (new:HEAD) / A---B---C (master) 

选项2:

  F (new:HEAD) / A---B---C (master) 

如何重新select选项1以及如何选项2?

从你的第一个图表(master = HEAD = F)到选项1:

 git branch new # Make a 'new' branch pointing at HEAD, which is F git reset --hard C # Move master back to point at C git checkout new # Make HEAD follow new, and get F in the working tree 

从选项1到选项2(从上面的选项中删除),

 git rebase -i master # Start the process of re-jiggering this branch # edit the commit list that opens up to only include F # save and exit # resolve potential conflicts from missing changes in D and E 

从您的出发点直接转到选项2:

 git checkout -b new C # Start the 'new' branch at C git cherry-pick F # Include F on it git checkout master # Switch back to master git reset --hard C # Rewind master to the earlier commit