You need to enable JavaScript to run this app. 如何将master分支的两次Commit迁移至新feature分支并Rebase
优惠活动
大模型
产品
解决方案
定价
更多

如何将master分支的两次Commit迁移至新feature分支并Rebase

解决方案:将master分支的Commit迁移至new-feature-branch

你可以通过git rebase --onto命令结合分支重置来实现需求,具体步骤如下:

  • 先确认当前处于master分支:

    git checkout master
    
  • 使用rebase --onto把C1、C2这两个提交迁移到new-feature-branch上:

    git rebase --onto new-feature-branch master~2 master
    

    这里的master~2指向C1提交之前的master分支节点(也就是初始状态里master分支在C1之前的最后一个提交),这条命令会把master分支上从master~2之后的所有提交(即C1、C2)重新应用到new-feature-branch的顶端。

  • 把master分支重置回原节点(移除C1、C2):

    git reset --hard master~2
    
  • 切换到new-feature-branch分支,确认提交已迁移完成:

    git checkout new-feature-branch
    

执行完以上步骤后,仓库结构就会达到你的期望状态:master分支回到C1、C2提交之前的版本,new-feature-branch分支顶端则是C1、C2两个提交。

内容的提问来源于stack exchange,提问作者ant_ren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 04:45:37