Git合并冲突解决:如何保留feature/branch-b的修改?
问题描述
执行以下Git操作:
git clone https://github.com/coderbyte-org/big-ocd big-ogit checkout -b feature/branch-aecho "This contents is beginning" > file.txtgit add file.txtgit commit -m "docs: add file.txt"git checkout -b feature/branch-becho "This is new content." > file.txtgit add file.txtgit commit -m "docs: update file.txt"git checkout feature/branch-aecho "This is another content" > file.txtgit merge feature/branch-bgit 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分支中的修改。
解决方案
暂存本地未提交修改
当前feature/branch-a分支的file.txt存在未提交修改,先暂存这些更改:git stash执行分支合并
现在可以正常发起合并:git merge feature/branch-b此时会触发合并冲突,因为两个分支都修改了
file.txt文件。保留feature/branch-b的修改版本
直接采用feature/branch-b分支中file.txt的内容:git checkout feature/branch-b -- file.txt提交合并结果
确认文件内容后,提交合并操作:git add file.txt git commit -m "merge: adopt feature/branch-b's file.txt changes"(可选)恢复暂存的修改
如果之前feature/branch-a的本地修改还需要,可以恢复并处理:git stash pop恢复后若出现冲突,手动调整文件内容后重新提交即可。
内容的提问来源于stack exchange,提问作者Byzantine
相关产品推荐
相关产品推荐

