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

发布于 2024-08-04 08:08:59 字数 590 浏览 7 评论 0原文

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

$ 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.

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

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

$ 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

谁对谁错谁最难过 2024-08-11 08:09:00

您有 2 个冲突:

  • res/layout/dialog_item.xml 在一个分支中更改,并在另一个分支中删除
  • src/DialogAdapter.java 在两个分支中更改

第二个你解决了。剩下的就是决定是否要删除 res/layout/dialog_item.xml 还是离开。在第一种情况下,您git rm它,在第二种情况下git add。然后执行git commit

重现冲突:

mkdir a
cd a
git init
echo a > a && echo b > b && git add . && git commit -m c1
git checkout -b f
echo af > a && rm b && git add -A && git commit -m cf
git checkout master
echo am > a && echo bm > b && git add -A && git commit -m cm
git merge f || true

You have 2 conflicts:

  • res/layout/dialog_item.xml was changed in one branch and deleted in the other one
  • src/DialogAdapter.java was changed in both branches

The second one you resolved. What's left is to decide whether you want to deleted res/layout/dialog_item.xml or leave. In the first case you git rm it, in the second git add. Then do git commit.

To reproduce the conflict:

mkdir a
cd a
git init
echo a > a && echo b > b && git add . && git commit -m c1
git checkout -b f
echo af > a && rm b && git add -A && git commit -m cf
git checkout master
echo am > a && echo bm > b && git add -A && git commit -m cm
git merge f || true
酒中人 2024-08-11 08:09:00

我遇到了类似的问题,但就我而言,这是对项目的拉取请求,该项目既不属于我,也没有写入权限。以下步骤会有所帮助:

  1. 将冲突的文件添加到本地。(res/layout/dialog_item.xml)
  2. 删除这些文件的全部内容,然后提交并提交。推动他们。
  3. 将弹出冲突警报,通过接受空文件来解决它。
  4. 从本地(dialog)中删除这些文件,然后推送到远程(master)(git rm path)。
    现在,您将看到冲突已解决。

I stuck with similiar problem but in my case it was a pull request on project that is neither owned by me nor i have write access. Following steps will be helpful:

  1. Add conflicting files to local.(res/layout/dialog_item.xml)
  2. Remove whole content of these files, then commit & push them.
  3. Conflicting alert will pop up, resolve it by accepting empty files.
  4. Removed these files from local(dialog) then push to remote(master) (git rm path).
    Now, you will be see conflicts are resolved.
倒数 2024-08-11 08:08:59

冲突消息:

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

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

因此,您必须决定是

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

,还是

  • 使用“git add res/layout/dialog_item”从 HEAD 接受版本(可能在编辑后) .xml

然后用“git commit”完成合并。

请注意,git 会警告您正在创建合并提交,在(罕见)情况下,这是您不想要的。可能是上述病例不太罕见的时候遗留下来的。

The conflict message:

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

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

So you have to decide whether

  • remove file using "git rm res/layout/dialog_item.xml"

or

  • accept version from HEAD (perhaps after editing it) with "git add res/layout/dialog_item.xml"

Then you finalize merge with "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.

扬花落满肩 2024-08-11 08:08:59

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

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

yes d | git mergetool -- the/subdirectory

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

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

I normally just run git mergetool and 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.

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 d is provided to choose deleting each file. You can also use m to keep the modified file. Taken from the prompt you see when you run mergetool:

Use (m)odified or (d)eleted file, or (a)bort?
筱果果 2024-08-11 08:08:59

如果您在 Windows 上使用 Git Gui,

  1. 请中止合并
  2. 确保您位于目标分支上
  3. 从资源管理器中删除冲突文件
  4. 重新扫描 Git Gui 中的更改 (F5)
  5. 注意冲突文件已被删除
  6. 选择暂存更改的文件以提交 (Ctrl- I)从“提交”菜单
  7. 输入提交注释,例如“已删除冲突文件”
  8. 提交(ctrl-enter)
  9. 现在,如果您重新启动合并,它将(希望)起作用。

If you are using Git Gui on windows,

  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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文