git - merge conflict when local is deleted but file exists in remote

Git

Git Problem Overview


I am very new to git and wondered how I should go about a merge where in the local repo I have deleted several files on the master branch but these files exist within the remote master branch.

After doing git-merge it shows the conflicts that have occured.

Using git gui it shows that the local file is deleted, while the remote branch file has contents.

How do you stop these files from being conflicted? Is there a simple way using git gui?

Many thanks

Git Solutions


Solution 1 - Git

You should resolve the conflicts as you see fit. If the file really is supposed to be removed, and you will be publishing that change to origin, remove it again:

git rm path/to/file

If the file should in fact be tracked still, add it (the version in the work tree will be the version from origin):

git add path/to/file

After doing either of those to resolve the conflict, commit the merge.

Solution 2 - Git

As an added tip in addition to the accepted answer, in a "deleted by us", if you would like to see the changes that were made to the deleted file so that you may apply those changes elsewhere you can use:

git diff ...origin/master -- path/to/file

If it is a "deleted by them" scenario, and you would like to see the changes so you can apply them elsewhere, you can use:

git diff origin/master... -- path/to/file

Solution 3 - Git

In Git GUI, you select the conflicted file and then right-click on the main text area where the conflicted text is shown.

In the context menu that appears, you can choose to go with "Remote" or go with "Local". So if a file is remotely deleted, you can choose "Remote" to propagate the delete locally, and vice versa.

Took me a month to figure it out...it would be nice if Git GUI actually had documentation...

Solution 4 - Git

The best-rated answer focuses on the way to resolve the conflict.

Before that, you probably want to know what the remote changed in the locally removed files.

To do that, you can see the changes with:

git diff --base

From https://git-scm.com/docs/git-diff#Documentation/git-diff.txt--1--base

> Compare the working tree with the "base" version [...]. The index contains these stages only for unmerged entries i.e. while resolving conflicts.

Solution 5 - Git

In EGit I also found problems. My solution was:

  • Used the Git Staging view.
  • Double clicked on each files shown on unstaged changes to open comparator
  • Click on the "Copy all from left to right" icon
  • Save file (it will disappear from the unstaged list)

Solution 6 - Git

As stated in this useful answer to a related question, you can use git mergetool to start a dialog where you can select if you want to take the modified or the deleted version of the file(s).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbinarycreationsView Question on Stackoverflow
Solution 1 - GitCascabelView Answer on Stackoverflow
Solution 2 - GitJoseph RavenwolfeView Answer on Stackoverflow
Solution 3 - GitJoel FreemanView Answer on Stackoverflow
Solution 4 - GitDorian MarchalView Answer on Stackoverflow
Solution 5 - GitborjabView Answer on Stackoverflow
Solution 6 - GitAmos EgelView Answer on Stackoverflow