我想将我承诺的最后几次提交移动到一个新的分支,并在提交之前将 master 带回。不幸的是,我的 Git-fu 不够强大,有什么帮助吗? + j4 j/ N1 m: _也就是说,我怎么能从这里开始- m0 f n/ D u3 b. t
master A - B - C - D - E到这个? - a+ |1 [) S2 _: Bnewbranch C - D - E master A - B 7 a2 f/ P/ g3 _% I6 ^+ p 解决方案: " [0 D1 o7 u5 b 转移到现有分支机构如果您想将提交移动到现有分支,如下所示: : Y2 m- p$ }5 A. s5 a6 egit checkout existingbranchgit merge mastergit checkout mastergit reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.git checkout existingbranch在执行此操作之前,您可以使用它git stash. 完成后,您可以使用以下命令检索隐藏的未提交编辑git stash pop2 ^: y6 Y3 q: {0 _# O% n
提交到新的分行警告:*这种方法是有效的,因为使用第一个命令创建一个新的分支:git branch newbranch。移动提交现有分支,实施前需要将变更合并到现有分支git reset --hard HEAD~3(请参阅以上内容移动到现有分支)。*如果不先合并更改,就会丢失。 - H: R; J' ^- j8 e" j除非涉及其他情况,否则可以通过分支和回滚轻松完成。 " v3 S9 F, A) J
# Note: Any changes not committed will be lost.git branch newbranch # Create a new branch,saving the desired commitsgit reset --hard HEAD~3 # Move master back by 3 commits (Make sure you know how many commits you need to go back)git checkout newbranch # Go to the new branch that still has the desired commits : Q1 r& v! o/ Q, B