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

使用git merge --no-commit后,如何保留当前分支的旧文件版本?

解决Git合并时保留当前分支版本的问题

场景回顾

在branch1分支执行git merge master --no-commit后,出现以下状态:

You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)

Changes to be committed:
new file: own_components/circuit_simulation/component/README.algorithm
modified: own_components/circuit_simulation/component/knot.h

Unmerged paths:
(use "git add ..." to mark resolution)
both modified: own_components/circuit_simulation/component/reordered_actions.h
both modified: project_m/s60sps/local_reordered_actions.h

需要保留branch1分支的旧版本,但执行git checkout或git checkout --ours命令无效果,下面分两类文件给出解决方案:


一、处理「Changes to be committed」中的文件(如knot.h)

这类文件已被Git暂存为合并后的状态,直接checkout无效,需先取消暂存再检出当前分支版本:

  1. 取消暂存目标文件:
    git reset HEAD own_components/circuit_simulation/component/knot.h
    
  2. 检出branch1分支的旧版本:
    git checkout own_components/circuit_simulation/component/knot.h
    

执行完两步后,该文件将回到branch1合并前的状态。


二、处理「Unmerged paths」中的冲突文件(如local_reordered_actions.h)

对于双端修改的冲突文件,直接用git checkout --ours无效时,可直接从当前分支(HEAD)检出版本,再标记冲突解决:

  1. 检出branch1分支的旧版本:
    git checkout HEAD -- project_m/s60sps/local_reordered_actions.h
    
  2. 标记该文件冲突已解决:
    git add project_m/s60sps/local_reordered_actions.h
    

重复上述步骤即可处理另一个冲突文件reordered_actions.h。


验证与完成

处理完所有文件后,执行git status确认状态正常,随后可执行git commit完成合并,或根据需求调整操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 16:12:43