合并master到老旧home-base分支时的Git文件位置冲突问题咨询
仓库背景与日常工作流程
- 维护一个存储Markdown文章的Git仓库,日常工作流程为:每篇Markdown文章对应一个Pull Request,所有PR最终合并到master分支后,会直接在master分支操作,将已发布的文章移动到wordpressed目录下,再提交推送到远程master。
- 目前有一个遗留很久的Pull Request存放在home-base分支中,该分支包含dan/homebase.md文件,计划后续补充提交完善该文件内容后,再将其合并到master分支。
- 由于home-base分支年代久远,站在该分支视角看,仓库结构和当前master分支的结构差异极大,因此尝试将master反向合并到home-base分支,以此拉齐仓库状态、将合并基线前移,但执行合并操作时遇到了无法理解的合并冲突。
仓库各节点的目录结构说明
master分支目录结构
$ git status On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean $ ls -1 -R README.md dan instructions.md wordpressed ./dan: ./wordpressed: AppDelegateRefactor.md BareListContentView.md CellContentConfiguration.md ControlTargetActioniOS14.md GetStartedWithPods.md MultipleTrailingClosures.md StatesAndBackgrounds.md Swift52notes1.md Swift52notes2.md Swift52notes3.md UserInteractiveCells.md bdd-common-pitfalls.md bdd.md brantsMenuBar.md collectionViewContentConfig.md collectionViewLists.md collectionViewLists2.md collectionViewOutline.md debuggingLinks.md diffableDataSources.md diffableDataSources2.md forwardCompatibity.md haptics.md iOS13deprecations.md images inAppPurchases.md logging.md miscSwiftTricks1.md miscSwiftTricks2.md miscSwiftTricks3.md miscSwiftTricks4.md multipleSelection.md mysteriesOfLayout.md newInIOS14.md packages.md photoPicker.md protocolParadox.md splitViewControllers.md splitViewControllers2.md swiftTrickTwoClasses.md targetActionRant.md typescript-shape-of-things.md untappableButton.md what-vs-how.md xcode12Editing.md xcode12testing.md xcodeWhereAmI.md xcodeWorkInTwoPlaces.md ./wordpressed/images: argumentsInScheme.png bigFlags.png callHierarchy.png callersMenu.png chooser.png config.png createdTester.png dataOrUrl.png ipadportraitoverlay.png listOfPepBoys.png listWithSectionHeaders.png littleFlags.png looksLikeADuck.png newTester.png outlineIndicatorsWrong.png outlineIndicatorsWrong2.png perfectAfterHack.png pickAPeppa.png rootOnly.png sandboxAccountOnDevice.png selfSizingStringDrawer.png simpleList.png split1.png split2.png splitViewControllerPadLandscape.png tableViewAsChoice.png threecolumns.png twoPossibilities.png twoscreensphone.png variableRowHeights.png workingOutline.png
从目录结构可以看到,仓库根目录目前基本为空,所有文章都存放在wordpressed目录下,配套图片则存放在wordpressed/images目录中。
home-base分支目录结构
$ git switch home-base Switched to branch 'home-base' Your branch is up to date with 'origin/home-base'. $ ls -1 -R GetStartedWithPods.md README.md Swift52notes1.md Swift52notes2.md Swift52notes3.md dan debuggingLinks.md images logging.md miscSwiftTricks1.md miscSwiftTricks2.md miscSwiftTricks3.md miscSwiftTricks4.md packages.md wordpressed xcode12Editing.md xcode12testing.md xcodeWhereAmI.md xcodeWorkInTwoPlaces.md ./dan: bdd.md homebase.md ./images: chooser.png config.png split1.png split2.png ./wordpressed: images ./wordpressed/images:
这是仓库早期的状态,当时所有文件都还没有被移动到wordpressed目录,少数文章直接存放在仓库根目录,dan目录下有两个文件,其中就包括需要维护的dan/homebase.md。
两个分支的共同合并基线目录结构
$ git merge-base master home-base b5d7355fe42eddad96beb200df2cba65381c288a $ git checkout b5d7355fe $ ls -1 -R GetStartedWithPods.md README.md Swift52notes1.md Swift52notes2.md Swift52notes3.md dan debuggingLinks.md images logging.md miscSwiftTricks1.md miscSwiftTricks2.md miscSwiftTricks3.md miscSwiftTricks4.md packages.md wordpressed xcode12Editing.md xcode12testing.md xcodeWhereAmI.md xcodeWorkInTwoPlaces.md ./dan: bdd.md ./images: chooser.png config.png split1.png split2.png ./wordpressed: images ./wordpressed/images:
该合并基线的目录结构和当前home-base分支的结构几乎一致,唯一区别是这个基线时间点下,dan目录中还没有出现homebase.md文件。
合并操作的预期与实际结果
按照常规理解,执行master合并到home-base的操作时,双方的变更本不应该产生冲突:
- master侧的变更是新增了大量wordpressed目录下的文件,同时将原根目录、原dan目录下的部分文件重命名移动到了wordpressed下
- home-base侧的变更是在dan目录下新增了homebase.md文件
两类变更没有重叠,本应可以自动合并完成。但实际执行合并操作时返回了如下结果:
$ git switch home-base $ git merge master CONFLICT (file location): dan/homebase.md added in HEAD inside a directory that was renamed in master, suggesting it should perhaps be moved to wordpressed/homebase.md. Automatic merge failed; fix conflicts and then commit the result. $ git status On branch home-base Your branch is up to date with 'origin/home-base'. You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Changes to be committed: deleted: dan/homebase.md new file: instructions.md new file: wordpressed/AppDelegateRefactor.md new file: wordpressed/BareListContentView.md new file: wordpressed/CellContentConfiguration.md new file: wordpressed/ControlTargetActioniOS14.md renamed: GetStartedWithPods.md -> wordpressed/GetStartedWithPods.md new file: wordpressed/MultipleTrailingClosures.md new file: wordpressed/StatesAndBackgrounds.md renamed: Swift52notes1.md -> wordpressed/Swift52notes1.md renamed: Swift52notes2.md -> wordpressed/Swift52notes2.md renamed: Swift52notes3.md -> wordpressed/Swift52notes3.md new file: wordpressed/UserInteractiveCells.md new file: wordpressed/bdd-common-pitfalls.md renamed: dan/bdd.md -> wordpressed/bdd.md new file: wordpressed/brantsMenuBar.md new file: wordpressed/collectionViewContentConfig.md new file: wordpressed/collectionViewLists.md new file: wordpressed/collectionViewLists2.md new file: wordpressed/collectionViewOutline.md renamed: debuggingLinks.md -> wordpressed/debuggingLinks.md new file: wordpressed/diffableDataSources.md new file: wordpressed/diffableDataSources2.md new file: wordpressed/forwardCompatibity.md new file: wordpressed/haptics.md new file: wordpressed/iOS13deprecations.md new file: wordpressed/images/argumentsInScheme.png new file: wordpressed/images/bigFlags.png new file: wordpressed/images/callHierarchy.png new file: wordpressed/images/callersMenu.png renamed: images/chooser.png -> wordpressed/images/chooser.png renamed: images/config.png -> wordpressed/images/config.png new file: wordpressed/images/createdTester.png new file: wordpressed/images/dataOrUrl.png new file: wordpressed/images/ipadportraitoverlay.png new file: wordpressed/images/listOfPepBoys.png new file: wordpressed/images/listWithSectionHeaders.png new file: wordpressed/images/littleFlags.png new file: wordpressed/images/looksLikeADuck.png new file: wordpressed/images/newTester.png new file: wordpressed/images/outlineIndicatorsWrong.png new file: wordpressed/images/outlineIndicatorsWrong2.png new file: wordpressed/images/perfectAfterHack.png new file: wordpressed/images/pickAPeppa.png new file: wordpressed/images/rootOnly.png new file: wordpressed/images/sandboxAccountOnDevice.png new file: wordpressed/images/selfSizingStringDrawer.png new file: wordpressed/images/simpleList.png renamed: images/split1.png -> wordpressed/images/split1.png renamed: images/split2.png -> wordpressed/images/split2.png new file: wordpressed/images/splitViewControllerPadLandscape.png new file: wordpressed/images/tableViewAsChoice.png new file: wordpressed/images/threecolumns.png new file: wordpressed/images/twoPossibilities.png new file: wordpressed/images/twoscreensphone.png new file: wordpressed/images/variableRowHeights.png new file: wordpressed/images/workingOutline.png new file: wordpressed/inAppPurchases.md renamed: logging.md -> wordpressed/logging.md renamed: miscSwiftTricks1.md -> wordpressed/miscSwiftTricks1.md renamed: miscSwiftTricks2.md -> wordpressed/miscSwiftTricks2.md renamed: miscSwiftTricks3.md -> wordpressed/miscSwiftTricks3.md renamed: miscSwiftTricks4.md -> wordpressed/miscSwiftTricks4.md new file: wordpressed/multipleSelection.md new file: wordpressed/mysteriesOfLayout.md new file: wordpressed/newInIOS14.md renamed: packages.md -> wordpressed/packages.md new file: wordpressed/photoPicker.md new file: wordpressed/protocolParadox.md new file: wordpressed/splitViewControllers.md new file: wordpressed/splitViewControllers2.md new file: wordpressed/swiftTrickTwoClasses.md new file: wordpressed/targetActionRant.md new file: wordpressed/typescript-shape-of-things.md new file: wordpressed/untappableButton.md new file: wordpressed/what-vs-how.md renamed: xcode12Editing.md -> wordpressed/xcode12Editing.md renamed: xcode12testing.md -> wordpressed/xcode12testing.md renamed: xcodeWhereAmI.md -> wordpressed/xcodeWhereAmI.md renamed: xcodeWorkInTwoPlaces.md -> wordpressed/xcodeWorkInTwoPlaces.md Unmerged paths: (use "git add <file>..." to mark resolution) added by us: wordpressed/homebase.md
完全无法理解冲突提示的含义:master和home-base两个分支都不存在wordpressed/homebase.md文件,明明是在home-base分支的dan目录下新增的homebase.md,Git判定这个文件应该被移动到wordpressed目录下,完全找不到对应的判断依据。
核心咨询问题
- Git提示“dan/homebase.md是在HEAD分支新增的文件,而它所在的目录在master分支中被重命名,因此建议将该文件移动到wordpressed/homebase.md”的底层逻辑是什么?为什么Git会做出这类无依据的文件移动判断?
- 该如何避免这类不必要的合并冲突,能否通过切换其他合并策略让Git的行为更简单可预期?
- 该如何手动解析这个冲突,直接保留dan/homebase.md的原有路径不被移动?
内容的提问来源于stack exchange,提问作者matt
相关产品推荐
相关产品推荐

