如何处理/修复 git add/add 冲突?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19475387/
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 17:09:39  来源:igfitidea点击:

How to handle/fix git add/add conflicts?

git

提问by Biggie Mac

I'm not sure when this error occurs, I haven't found any descriptions in google.

我不确定这个错误何时发生,我在谷歌中没有找到任何描述。

So my colleagues commited some new files and changed files on branch1. I then got these changes and merged them into my branch(branch2) but NOT using git merge but manually using beyond compare (I know it's a bad practice merging manually). However, after merging them manually and copy-pasting the new files in my branch as well, I commited the work in branch2. However, now, when they tried to get some changes from me using git merge origin/branch2 they receive a lot of git "add/add" conflicts on the new files they initially added.

所以我的同事提交了一些新文件并更改了 branch1 上的文件。然后我得到了这些更改并将它们合并到我的分支(branch2)中,但不使用 git merge 而是手动使用 Beyond compare(我知道手动合并是一种不好的做法)。但是,在手动合并它们并将新文件复制粘贴到我的分支后,我在 branch2 中提交了工作。但是,现在,当他们尝试使用 git merge origin/branch2 从我那里获得一些更改时,他们在他们最初添加的新文件上收到了很多 git "add/add" 冲突。

Can anybody tell me why git sees these files as conflicts although they are the same? And how should these conflicts be handled?

谁能告诉我为什么 git 将这些文件视为冲突,尽管它们是相同的?以及应该如何处理这些冲突?

回答by janos

You get CONFLICT (add/add): Merge conflict in ...conflicts because the files are in fact not the same. You can see the difference using the diffcommand, for example:

你得到CONFLICT (add/add): Merge conflict in ...冲突,因为文件实际上不一样。您可以使用diff命令查看差异,例如:

git diff branch1 branch2 -- path/to/file

where branch1and branch2are the names of the branches you want to compare.

其中branch1branch2是要比较的分支的名称。

Or, an easier way to see the difference, right after the failed git mergewhen you are in the conflicted state, you can simply run git diffwithout parameters, it will reveal the conflicted regions.

或者,一种更简单的查看差异的方法,git merge在您处于冲突状态时失败后,您可以简单地git diff不带参数运行,它会显示冲突区域。

To resolve such conflict, you have to decide which branch has the right content. You can do that by checking out the file from the appropriate branch, for example:

要解决此类冲突,您必须决定哪个分支具有正确的内容。您可以通过从适当的分支检出文件来做到这一点,例如:

git checkout name_of_branch path/to/file

Sometimes the right content is a mix of the two files. In that case you have to resolve manually, possibly by studying the differences using the git diffcommand, as in the examples above.

有时,正确的内容是两个文件的混合。在这种情况下,您必须手动解决,可能通过使用git diff命令研究差异,如上例所示。

The long-term solution is to avoid making conflicting changes to the same paths in different branches, which is typically a task sharing / communication issue between collaborators.

长期的解决方案是避免对不同分支中的相同路径进行冲突更改,这通常是协作者之间的任务共享/通信问题。

回答by Dana

I had a git (Add/Add) conflict. The information from Janos provided the insight needed for a solution. The files "appeared" identical, but my conflicting folder of files were added with a permission of 664 on one branch, and a permission of 755 on another branch. Using git statusrevealed the difference between the files.

我有一个 git(添加/添加)冲突。Janos 提供的信息提供了解决方案所需的洞察力。这些文件“看起来”相同,但我的冲突文件文件夹在一个分支上以 664 的权限添加,在另一个分支上以 755 的权限添加。使用git status揭示了文件之间的差异。

Steps taken to resolve the conflict:

解决冲突的步骤

git merge --abort
rm -rfv myconflictedfolder
git commit -m'Resolve git Add/Add branch merge conflict by deleting conflicted folder and files in myconflictedfolder'
git merge mybranch