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

Git合并冲突解决:如何保留feature/branch-b的修改?

问题描述

执行以下Git操作:

  • git clone https://github.com/coderbyte-org/big-o
  • cd big-o
  • git checkout -b feature/branch-a
  • echo "This contents is beginning" > file.txt
  • git add file.txt
  • git commit -m "docs: add file.txt"
  • git checkout -b feature/branch-b
  • echo "This is new content." > file.txt
  • git add file.txt
  • git commit -m "docs: update file.txt"
  • git checkout feature/branch-a
  • echo "This is another content" > file.txt
  • git merge feature/branch-b
  • git show HEAD:file.txt && git show --pretty='' HEAD^1

执行后出现错误:

error: Your local changes to the following files would be overwritten by merge:
file.txt
Please commit your changes or stash them before you merge.
Aborting

需要解决该合并冲突并保留feature/branch-b分支中的修改。

解决方案
  1. 暂存本地未提交修改
    当前feature/branch-a分支的file.txt存在未提交修改,先暂存这些更改:

    git stash
    
  2. 执行分支合并
    现在可以正常发起合并:

    git merge feature/branch-b
    

    此时会触发合并冲突,因为两个分支都修改了file.txt文件。

  3. 保留feature/branch-b的修改版本
    直接采用feature/branch-b分支中file.txt的内容:

    git checkout feature/branch-b -- file.txt
    
  4. 提交合并结果
    确认文件内容后,提交合并操作:

    git add file.txt
    git commit -m "merge: adopt feature/branch-b's file.txt changes"
    
  5. (可选)恢复暂存的修改
    如果之前feature/branch-a的本地修改还需要,可以恢复并处理:

    git stash pop
    

    恢复后若出现冲突,手动调整文件内容后重新提交即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 15:07:39