Git Merge Conflict - Remote File Deleted, Local File Changed

19,187

Solution 1

Try using the --force parameter:

git rm --force <file>

If you want to keep the file in filesystem:

git rm --cached <file>

Solution 2

I tried git rm path/to/file but it says file: needs merge

You will no longer see that error message with Git 2.23 (Q3 0219, seven years later)

"git rm" resolving a conflicted path used to leak an internal message "needs merge" before actually removing the path, which was confusing.
This has been corrected.

See commit b2b1f61 (17 Jul 2019) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 5e9d978, 25 Jul 2019)

rm: resolving by removal is not a warning-worthy event

When resolving a conflict on a path in favor of removing it, using "git rm" on it is the standard way to do so.
The user however is greeted with a "needs merge" message during that operation:

$ git merge side-branch
$ edit conflicted-path-1
$ git add conflicted-path-1
$ git rm conflicted-path-2
conflicted-path-2: needs merge
rm 'conflicted-path-2'

The removal by "git rm" does get performed, but an uninitiated user may find it confusing:
"needs merge? So I need to resolve conflict before being able to remove it???"

The message is coming from "update-index --refresh" that is called internally to make sure "git rm" knows which paths are clean and which paths are dirty, in order to prevent removal of paths modified relative to the index without the "-f" option.

We somehow ended up not squelching this message which seeped through to the UI surface.

Use the same mechanism used by "git commit", "git describe", etc. to squelch the message.

Share:
19,187

Related videos on Youtube

xylar
Author by

xylar

Updated on June 07, 2022

Comments

  • xylar
    xylar about 2 years

    I merged in changes from another branch but I am getting conflicts because a file has been deleted on the remote branch but has changes on local branch.

    I would like to delete the file - I tried git rm path/to/file but it says file: needs merge. What is the best way of removing the file and committing the merge?

  • Andrew Bullock
    Andrew Bullock about 8 years
    got the same problem, but --force doesnt work, it still says "needs merge" for the file(s)
  • Nava Polak Onik
    Nava Polak Onik over 7 years
    It is working, but isn't there is a way to do it for all files deleted?
  • fork0
    fork0 over 7 years
    Of course. You can use git diff --diff-filter=D --name-only -z | xargs -0 git rm --force --cached
  • fork0
    fork0 over 7 years
    @AndrewBullock it says that, but it still deletes the file. It's diagnostics, which some may consider to be a bit excessive.
  • fork0
    fork0 over 7 years
    @NavaPolakOnik er. Make it git diff --diff-filter=D --name-only -z HEAD..MERGE_HEAD | xargs -0 git rm --force --cached
  • Lii
    Lii over 5 years
    This would be a much better answer it you explained the command you're using!