git 如何解决由于删除分支中的文件而导致的合并冲突?

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

How do I fix a merge conflict due to removal of a file in a branch?

gitgit-mergemerge-conflict-resolution

提问by Jakub Nar?bski

I have create a dialogbranch and when I try to merge it to masterbranch. There are 2 conflicts. I don't know how to resolve CONFLICT (delete/modify). Can you please tell me what to do?

我创建了一个dialog分支,当我尝试将它合并到master分支时。有2个冲突。我不知道如何解决CONFLICT (delete/modify)。你能告诉我该怎么做吗?

$ git checkout master
$ git merge dialog
CONFLICT (delete/modify): res/layout/dialog_item.xml deleted in dialog and modified in HEAD. Version HEAD of res/layout/dialog_item.xml left in tree.
Auto-merging src/com/DialogAdapter.java
CONFLICT (content): Merge conflict in src/DialogAdapter.java
Automatic merge failed; fix conflicts and then commit the result.

I have opened src/DialogAdapter.java, fixed the conflict and did a git add src/DialogAdapter.java. What else do I need to do?

我已经打开src/DialogAdapter.java,修复了冲突并做了一个git add src/DialogAdapter.java. 我还需要做什么?

回答by Jakub Nar?bski

The conflict message:

冲突消息:

CONFLICT (delete/modify): res/layout/dialog_item.xml deleted in dialog and modified in HEAD

means that res/layout/dialog_item.xmlwas deleted in the 'dialog' branch you are merging, but was modified in HEAD (in the branch you are merging to).

表示res/layout/dialog_item.xml已在您正在合并的“对话框”分支中删除,但在 HEAD(在您要合并到的分支中)中进行了修改。

So you have to decide whether

所以你必须决定是否

  • remove file using "git rm res/layout/dialog_item.xml"
  • 使用“ git rm res/layout/dialog_item.xml”删除文件

or

或者

  • accept version from HEAD (perhaps after editing it) with "git add res/layout/dialog_item.xml"
  • 使用“ git add res/layout/dialog_item.xml”接受来自 HEAD 的版本(可能在编辑之后)

Then you finalize merge with "git commit".

然后你完成与“ git commit”的合并。

Note that git will warn you that you are creating a merge commit, in the (rare) case where it is something you don't want. Probably remains from the days where said case was less rare.

请注意,在(罕见的)情况下,git 会警告您正在创建合并提交,这是您不想要的。可能是从那个案例不那么罕见的日子开始的。

回答by void.pointer

I normally just run git mergetooland it will prompt me if I want to keep the modified file or keep it deleted. This is the quickest way IMHO since it's one command instead of several per file.

我通常只是运行git mergetool,它会提示我是否要保留修改后的文件或将其删除。恕我直言,这是最快的方式,因为它是一个命令,而不是每个文件的多个命令。

If you have a bunch of deleted files in a specific subdirectory and you want all of them to be resolved by deleting the files, you can do this:

如果您在特定子目录中有一堆已删除的文件,并且您希望通过删除文件来解决所有这些文件,您可以这样做:

yes d | git mergetool -- the/subdirectory

The dis provided to choose deleting each file. You can also use mto keep the modified file. Taken from the prompt you see when you run mergetool:

d提供给选择删除每个文件。您也可以使用m来保留修改后的文件。从您运行时看到的提示中获取mergetool

Use (m)odified or (d)eleted file, or (a)bort?

回答by salihcenap

If you are using Git Gui on windows,

如果你在 Windows 上使用 Git Gui,

  1. Abort the merge
  2. Make sure you are on your target branch
  3. Delete the conflicting file from explorer
  4. Rescan for changes in Git Gui (F5)
  5. Notice that conflicting file is deleted
  6. Select Stage Changed Files To Commit (Ctrl-I) from Commit menu
  7. Enter a commit comment like "deleted conflicting file"
  8. Commit (ctrl-enter)
  9. Now if you restart the merge it will (hopefully) work.
  1. 中止合并
  2. 确保您在目标分支上
  3. 从资源管理器中删除冲突文件
  4. 重新扫描 Git Gui (F5) 中的更改
  5. 注意冲突文件被删除
  6. 从 Commit 菜单中选择 Stage Changed Files To Commit (Ctrl-I)
  7. 输入诸如“已删除冲突文件”之类的提交注释
  8. 提交 (ctrl-enter)
  9. 现在,如果您重新启动合并,它将(希望)起作用。