【问题标题】:git: how to move some commits to new branchgit:如何将一些提交移动到新分支
【发布时间】:2011-03-11 05:58:15
【问题描述】:

我一直在直线工作:

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?

【问题讨论】:

    标签: git version-control


    【解决方案1】:

    从您的第一个图表(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
    

    【讨论】:

    • 感谢您的详细回答。
    • 谢谢 :) 另外,为“re-jiggering”+1!
    • 如果您在尝试推送 master 时收到错误“更新被拒绝,因为您当前分支的尖端落后于其远程副本”,您需要使用 --force 选项:git push --force origin master
    猜你喜欢
    • 1970-01-01
    • 2012-09-22
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 2017-02-01
    相关资源
    最近更新 更多