Git“建议移动[文件]”导致的Git文件级合并冲突 [英] Git file-level merge conflict caused by Git “suggesting [the file] should perhaps be moved”

查看:11
本文介绍了Git“建议移动[文件]”导致的Git文件级合并冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Markdown文章的Git资源库,在那里人们创建拉取请求(每个Markdown文章一个),这些请求最终被合并到master,然后,直接在master上工作,我将该文章移动到一个名为WordPress的文件夹中,然后提交并推送到master

我有一个非常老的拉取请求,它仍然位于名为home-base的分支中,由一个文件Dan/homebase.md组成,我正在考虑通过一些提交将其编辑成形状,然后合并到master

这个home-base分支非常太老了,从它的角度来看,repo与master中今天的样子完全不同。因此,我认为将master反向合并到home-base可能会更好,这样可以使其更新,并将合并基准向上移动很多。

但当我尝试执行此操作时,遇到无法理解的合并冲突。


为了向您展示这种情况,我将展示每个分支中的ls文件和文件夹的样子。我将从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

如您所见,主文件夹本身基本上是空的。所有文章都以文字按下的表示,它们的图片也以文字按下/图像的形式显示。


好的,现在是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:

如您所见,这是一个更早的情况。尚未将任何内容移动到WordPress。有几篇文章漂浮在顶层,Dan文件夹有两篇文章,其中包括我在这里感兴趣的home base.md


最后,为了完整起见,我将向您展示masterhome-base之间的合并基数:

$ 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文件夹中出现home base.md之前。


现在。请问问自己,当我尝试将master合并到home-base时,会发生什么情况。合并双方的贡献是什么?在我看来,Git应该意识到,在master中,wordPress中出现了许多新文件,其中一些文件是过去位于顶级的文件的重命名。当然,在home-base中,Dan中出现了一个新文件home base.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 

好的,最后一行是我不明白的。说我们添加的wordPress/homebase.md是什么意思?不是!这不是我们添加它的地方。我们已将其添加到中。

masterhome-base都没有文件wordPress/home base.md。那么,Git是从哪里得到这样的想法的呢?这似乎与它认为存在合并冲突的原因相同:

Dan/homebase.md在master中重命名的目录中添加了head,建议可能应将其移动到wordPress/home base.md。

那么:Git在这里说的是什么?为什么Git认为Dan/homebase.md应该是word/homebase.md,没有我能找到的任何证据?我如何防止这种合并冲突,并让Git在这方面变得更加简单化?我可以使用不同的合并策略吗?或者,我如何才能简单地解决冲突,说,不,Git,请不要理会Dan/home base.md

再次对问题中的详细内容表示歉意。

推荐答案

此特定冲突:

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.

是Git中一个相对现代的发明。Git现在尝试在合并基本提交和每个提示提交之间查找目录重命名。也就是说,在运行git diff --find-renames <base> <left>git diff --find-renames <base> <right>之后,它在左侧检测到一些重命名,在右侧检测到一些重命名(总数为正数)。这些重命名中至少有一些暗示了目录名称的更改。因此,Git现在认为可能合并的另一端应该应用相同的目录名更改。

(Git预发行版大约在2岁左右添加了这段代码的各种实验版本,我忘记了它什么时候真正稳定下来的。仔细阅读发行说明,它看起来就像是2.17版本,然后是2.19版本中的后续版本。)

[编辑,2021年4月15日:]当这个新功能加入时,一个新的控制旋钮也加入了:merge.directoryRenames。它的默认值是conflict,但您可以将其设置为true(表示始终假设它已经在做,但不要称之为冲突)或false(表示永远不要做新的假设,因此不要称之为冲突)。

这是Git宣布的唯一冲突,因此它是显示在最终未合并文件和部分中的唯一文件:

Unmerged paths:
  (use "git add <file>..." to mark resolution)
    added by us:     wordpressed/homebase.md

Git在这里采纳了自己的内部建议:它认为,由于合并基和master之间的变化,添加到合并基和HEAD之间的所有内容都应该添加到wordpressed/中。

如果您认为这是正确的,Git希望您通过在该路径名上运行git add来确认这一点。如果没有,Git希望您将文件移动到您认为应该放到的任何位置,并git add结果(并使用git rm或可能使用git add删除Git使用的名称)。

这篇关于Git“建议移动[文件]”导致的Git文件级合并冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆