Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
git:如何将一些提交移动到新分支_Git_Version Control - Fatal编程技术网

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

git:如何将一些提交移动到新分支,git,version-control,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?要从您的第一张图表(

我一直在直线工作:

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?

要从您的第一张图表(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(从上面停止的地方开始)

要直接从起点转到选项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

谢谢你的详细回答。谢谢:)另外,+1用于“重新装配”!如果在尝试推送主机时出现错误“更新被拒绝,因为当前分支的尖端位于其远程对应分支的后面”,则需要使用--force选项:
git push--force origin master
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