You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

误在master分支提交代码,如何转移至new_feature分支并撤销原提交?

解决Git分支提交错误的操作步骤

步骤1:撤销master分支上的错误提交(保留改动)

  • 切换回master分支:
    git switch master
    
    (也可以用git checkout master,效果完全一致)
  • 执行软重置操作,撤销最近一次提交但保留所有代码改动:
    git reset HEAD~1 --soft
    
    这个命令会把master分支的HEAD指针回退到错误提交之前的版本,同时将错误提交里的代码改动保留在暂存区(若提交前已执行过git add)或工作区,不会丢失任何开发内容。

步骤2:将改动转移到new_feature分支并提交

  • 切换到new_feature分支:
    git switch new_feature
    
  • 确认改动同步后,完成提交:
    git add .
    git commit -m "你的新功能提交描述"
    
    (如果之前的改动已经在暂存区,可跳过git add .直接执行commit)

验证操作结果

执行git log分别查看master和new_feature分支的提交记录,确认master分支回到了错误提交前的状态,new_feature分支已包含新功能的提交即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 12:53:07