拉取GitHub远程windows分支时遇冲突,求解决方法
问题:拉取Windows分支时出现Git合并冲突
我参与一个项目,需要fork指定的GitHub仓库。计划在自己的fork版本中整合原仓库的windows分支——原仓库main分支适配Linux系统,windows分支适配我使用的Windows系统。但执行pull操作时出现冲突问题,此前fetch操作成功获取了该分支,且操作前我完全没打开或修改过任何代码。
以下是命令行操作记录:
C:\Users\mhmds>git clone https://github.com/MohamadShaheen/simulatorMapping.git Cloning into 'simulatorMapping'... remote: Enumerating objects: 1385, done. remote: Counting objects: 100% (314/314), done. remote: Compressing objects: 100% (65/65), done. remote: Total 1385 (delta 269), reused 275 (delta 249), pack-reused 1071 Receiving objects: 100% (1385/1385), 73.09 MiB | 3.06 MiB/s Receiving objects: 100% (1385/1385), 73.67 MiB | 3.79 MiB/s, done. Resolving deltas: 100% (631/631), done. C:\Users\mhmds>git clone https://github.com/microsoft/vcpkg.git Cloning into 'vcpkg'... remote: Enumerating objects: 199719, done. remote: Counting objects: 100% (14950/14950), done. remote: Compressing objects: 100% (407/407), done. remote: Total 199719 (delta 14661), reused 14641 (delta 14543), pack-reused 184769 Receiving objects: 100% (199719/199719), 64.29 MiB | 2.99 MiB/s, done. Resolving deltas: 100% (130594/130594), done. Updating files: 100% (10349/10349), done. C:\Users\mhmds>cd simulatorMapping C:\Users\mhmds\simulatorMapping>git fetch https://github.com/rbdlabhaifa/simulatorMapping.git windows:windows remote: Enumerating objects: 221, done. remote: Counting objects: 100% (221/221), done. remote: Compressing objects: 100% (46/46), done. Receiving objects: 89% (126/141), 190.18 MiB | 1.82 MiB/sreused 0 Receiving objects: 100% (141/141), 190.46 MiB | 2.91 MiB/s, done. Resolving deltas: 100% (95/95), completed with 66 local objects. From https://github.com/rbdlabhaifa/simulatorMapping * [new branch] windows -> windows C:\Users\mhmds\simulatorMapping>git pull https://github.com/rbdlabhaifa/simulatorMapping.git windows:windows Auto-merging CMakeLists.txt CONFLICT (content): Merge conflict in CMakeLists.txt Auto-merging Thirdparty/DBoW2/DBoW2/TemplatedVocabulary.h Auto-merging exe/CMakeLists.txt CONFLICT (add/add): Merge conflict in exe/CMakeLists.txt Auto-merging exe/runSimulator.cpp CONFLICT (content): Merge conflict in exe/runSimulator.cpp Auto-merging install.sh CONFLICT (content): Merge conflict in install.sh Auto-merging slam/src/LoopClosing.cc CONFLICT (content): Merge conflict in slam/src/LoopClosing.cc Auto-merging slam/src/System.cc CONFLICT (content): Merge conflict in slam/src/System.cc Auto-merging utils/include/Auxiliary.h Auto-merging vcpkg.json CONFLICT (add/add): Merge conflict in vcpkg.json Automatic merge failed; fix conflicts and then commit the result.
解决建议
原因说明
git pull本质是git fetch + git merge,你的本地main分支(来自fork的Linux适配版本)和远程windows分支是基于不同方向开发的,代码存在大量差异,Git无法自动完成合并,所以触发冲突,和你是否修改本地代码无关。
方案1:直接使用windows分支(最简单)
如果你只是想使用Windows适配的代码,不需要合并main分支的内容,直接切换到已拉取的windows分支即可:
git checkout windows
方案2:手动合并windows分支到main
如果需要将Windows适配的改动整合到你的main分支,需手动解决冲突:
- 确保当前处于main分支:
git checkout main - 执行合并操作(已fetch过,无需重复指定远程):
git merge windows - 打开所有标记冲突的文件,找到
<<<<<<<、=======、>>>>>>>分隔符,保留Windows适配的代码片段,删除冲突标记 - 解决完所有冲突后,提交合并结果:
git add . git commit -m "合并windows分支并解决冲突"
方案3:强制用windows分支覆盖main分支
如果完全不需要main分支的内容,想直接将main分支替换为windows分支的代码(此操作会丢弃main分支所有本地改动,谨慎使用):
git reset --hard windows
内容的提问来源于stack exchange,提问作者Straw User
相关产品推荐
相关产品推荐

