ChatGPT解决这个技术问题 Extra ChatGPT

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

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?

You can read this post on Stack Overflow: - How Do I Fix Merge Conflicts in Git? The accepted answer references the git manual.

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


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, stackoverflow.com/questions/19985906/… comes up again.
Never mind the second half of that comment. The output from Git is somewhat alarming but seems to be harmless.
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.
@mit: you can use git ls-files --stage and/or git status --porcelain and/or git diff-files <something> to get the list of changed files and drive a script to delete them or accept your or theirs version.
@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...
v
void.pointer

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?

@BrianMinton me too, the other stuff was just a vicious circle
This gets annoying when its many files, is there a way to give one answer for all?
@ideasman42 I have updated my answer to show a batch-delete solution.
I didn't really know about merge tool. You saved my day dude!
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
s
salihcenap

If you are using Git Gui on windows,

Abort the merge Make sure you are on your target branch Delete the conflicting file from explorer Rescan for changes in Git Gui (F5) Notice that conflicting file is deleted Select Stage Changed Files To Commit (Ctrl-I) from Commit menu Enter a commit comment like "deleted conflicting file" Commit (ctrl-enter) Now if you restart the merge it will (hopefully) work.