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

时间:2010-07-02 18:52:47

标签: git version-control

我一直在直线工作:

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)

如何重新定义选项1以及如何选择选项2?

1 个答案:

答案 0 :(得分:70)

从第一个图表(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
相关问题