283

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?

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
  • You can read this post on Stack Overflow: - [How Do I Fix Merge Conflicts in Git?](http://stackoverflow.com/questions/161813/how-do-i-fix-merge-conflicts-in-git) The accepted answer references the [git manual](http://www.kernel.org/pub/software/scm/git/docs/user-manual.html#resolving-a-merge). – heavyd Sep 04 '09 at 17:44
  • Possible duplicate of [git - merge conflict when local is deleted but file exists in remote](https://stackoverflow.com/questions/4319486/git-merge-conflict-when-local-is-deleted-but-file-exists-in-remote) – IMSoP Jul 19 '19 at 09:30

4 Answers4

354

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.

Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
  • 27
    Tried the former (`git rm …`) but I get `…: needs merge` and `rm '…'` which I am having a hard time interpreting. And then if I try to commit, http://stackoverflow.com/questions/19985906/git-merge-not-creating-merge-commit-no-merge-head comes up again. – Jesse Glick Mar 06 '14 at 17:43
  • 18
    Never mind the second half of that comment. The output from Git is somewhat alarming but seems to be harmless. – Jesse Glick Mar 06 '14 at 20:19
  • 5
    Is there a mass approach? I have a project at hand that relies on external stuff that I don't want to keep. So there are lots of files that were modified upstream and I want to delete in my branch that I'm rebasing no matter what. It is tedious to do it one by one. – mlt Jan 13 '15 at 22:27
  • 2
    @mit: you can use `git ls-files --stage` and/or `git status --porcelain` and/or `git diff-files ` to get the list of changed files and drive a script to delete them or accept your or theirs version. – Jakub Narębski Jan 13 '15 at 22:31
  • @jakub, that's an idea, but there should be a way to resolve automatically, what about git merge with the -Xtheirs or -Xours options? those don't seem to work for file deletions though – Alexander Mills Oct 26 '16 at 19:18
  • Say I do `git merge someBranch` and get this error. Right after I get an CONFLICT message. Right there I'm suppose to do either of deleting or accepting? does this mean that it knows which branch I'm referring to? because the changes could be on the other branch I'm merging from and in neither of your solutions there is an indication of branch. It's only file path... – mfaani Jan 21 '17 at 14:55
  • 1
    @Honey: The CONFLICT message includes description about which change was on which branch. You can check it later with `git status` or `git diff --cc`. There is also `git log --merge` which may help... – Jakub Narębski Jan 23 '17 at 22:58
  • I would like to have the behaviour, that files unstaged with rm are just deleted with every merge, although they are changed on that branch ... – Nikolai Ehrhardt Sep 10 '21 at 14:01
111

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?
void.pointer
  • 24,859
  • 31
  • 132
  • 243
  • 2
    I tried this, and got `This message is displayed because 'merge.tool' is not configured.` Adding the `-t emerge` param (and, later, running `git config --global merge.tool emerge`) fixed this for me – Steve Almond May 29 '21 at 09:44
  • @SteveAlmond You are correct. My answer assumes merge tool has already been configured, which it may not be for everyone. Thank you for adding context. – void.pointer Jun 20 '21 at 01:13
12

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.
salihcenap
  • 1,927
  • 22
  • 25
1

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
x-yuri
  • 16,722
  • 15
  • 114
  • 161