Git分支间同一文件合并咨询:自定义指定合并输出结果
Git分支合并实现指定文件内容的方案
方案一:手动合并(最可控)
- 切换到目标分支(比如要把结果保留在
branch1):git checkout branch1 - 执行合并命令,Git会检测到文件冲突:
git merge branch2 - 打开冲突文件,会看到Git标记的冲突区块:
<<<<<<< HEAD print("Hello world"); test(); ======= function1(); >>>>>>> branch2 - 手动修改为你需要的内容:
print("Hello world"); test(); function1(); - 标记冲突已解决并完成提交:
确认自动弹出的提交信息即可完成合并。git add 你的文件名 git commit
方案二:保留分支内容后手动追加
- 切换到
branch1:git checkout branch1 - 执行合并触发冲突:
git merge branch2 - 保留
branch1的文件内容:git checkout --ours 你的文件名 - 打开文件,将
branch2中的function1();追加到末尾,然后提交:git add 你的文件名 git commit -m "Merge branch2 content to end"
方案三:直接提取内容合并(跳过冲突流程)
- 切换到
branch1:git checkout branch1 - 提取
branch2的文件内容追加到当前文件:git show branch2:你的文件名 >> 你的文件名 - 提交修改:
git add 你的文件名 git commit -m "Add function1() from branch2"
内容的提问来源于stack exchange,提问作者Sujyrokimora
相关产品推荐
相关产品推荐

