git - 两个分支添加的相同文件导致奇怪的合并冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17640019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:33:21  来源:igfitidea点击:

git - Same file added by both branches causes weird merge conflict

gitgit-mergemerge-conflict-resolution

提问by BrainStone

I currently have two branches I am working on. Because of a software update I had to completly change the folder structre. Therefore I moves the files in both branches. Now I reached a point where I want to merge my working branch into my master branch.

我目前有两个分支我正在工作。由于软件更新,我不得不完全更改文件夹结构。因此我在两个分支中移动文件。现在我想将我的工作分支合并到我的主分支中。

The problem is that there are merge conflicts that tell me that a file was added by only one branch (added by themor added by us). The problem is that the file has been added by bothbranches.

问题是存在合并冲突,告诉我一个文件仅由一个分支(added by themadded by us)添加。问题是该文件已被两个分支添加。

For example I have a texture at textures/texture1.png. The master branch just moved it to the right location (was before misc/textures/texture1.png). The working branch moved it to the exact same location and edited it afterwards. The merge conflict for this file says:

例如,我在textures/texture1.png. master 分支只是将它移动到了正确的位置(之前是misc/textures/texture1.png)。工作分支将其移动到完全相同的位置,然后对其进行编辑。此文件的合并冲突说:

    added by us: textures/texture1.png

The point is that this is notthe file I want! I want the file from the other branch!
When I do

重点是这不是我要的文件!我想要另一个分支的文件!
当我做

git checkout --theirs textures/texture1.png

I get

我得到

error: path 'textures/texture1.png' does not have their veresion

But this file doesexist! I added it recently! And that's the file I want

但是这个文件确实存在!我最近添加了它!这就是我想要的文件

How do I resolve these conflicts?

我该如何解决这些冲突?

(more information if needed!)

(如果需要更多信息!)

采纳答案by mnagel

a--b--c--M--d--f--E--g--h--i  <<< master
    \
     \
      x--M'--y--z              <<< you

does your situation looks somewhat like above (where M and M' are the commits moving and E is the commit that edits the textures)? you try to merge z/i and git is not really happy.

您的情况是否看起来有点像上面(其中 M 和 M' 是移动的提交,而 E 是编辑纹理的提交)?你尝试合并 z/i 并且 git 不是很开心。

you might try to merge M with M' on a temporary branch

您可能会尝试在临时分支上将 M 与 M' 合并

a--b--c---M--d--f--E--g--h--i  <<< master
    \      \
     \      X                  <<< helper
      \    /
       x--M'--y--z             <<< you

and then merge helper ("X") with you ("z") and master ("i").

然后将助手(“X”)与您(“z”)和主人(“i”)合并。

a--b--c---M--d--f--E--g--h--i     <<< master
    \      \                 \ 
     \      X-----XX---------XXX  <<< helper
      \    /      /
       x--M'--y--z                <<< you

that way conflicts are resolved directly after they are created and not carried along. often this is easier because conflicts tend to grow over time.

这样,冲突在产生后直接解决,而不是继续下去。这通常更容易,因为冲突往往会随着时间的推移而增长。

回答by maximus ツ

You can always try

你可以随时尝试

git mergetool

this will open a GUI, where you can choose your desired changes by just clicking the appropriate links. Sometimes you need to do manual changes. But in your case you just have to select a file image.

这将打开一个 GUI,您可以在其中通过单击适当的链接来选择所需的更改。有时您需要进行手动更改。但在您的情况下,您只需要选择一个文件图像。

回答by user2683246

While resolving conflicts just use git checkoutindicating the right tree-ish(branch name, tag, sha1 of the commit...) from where you want to get a file:

在解决冲突时,只需使用git checkout指示从哪里获取文件的正确树状(分支名称、标签、提交的 sha1...):

git checkout theirBranchYouAreMerging -- textures/texture1.png

If you don't want all "their" changes, you could then cancel them partially. For that you would need git reset -pfor selectively removing any undesirable change from the index, and then git checkoutto remove them from the working directory as well:

如果您不想要所有“他们的”更改,则可以部分取消它们。为此,您需要git reset -p有选择地从索引中删除任何不需要的更改,然后git checkout也将它们从工作目录中删除:

git checkout theirBranch -- some/file.txt
git reset -p -- some/file.txt
git checkout -- some/file.txt