125

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

binarycreations
  • 3,091
  • 6
  • 32
  • 38
  • In the past, one thing I've done is to leave the "deleted" files in source control, but exclude them from the project/makefile/whatever. It's an okay temporary workaround for the merge conflicts, at least. – Mark Rushakoff Nov 30 '10 at 23:27
  • 2
    You fix it the same way you fix any merge conflict: add the desired version (either the file, or the lack of file) to the index, and then commit. Which one do you want? – Cascabel Dec 01 '10 at 00:02
  • Possible duplicate of [How do I fix a merge conflict due to removal of a file in a branch?](http://stackoverflow.com/questions/1380670/how-do-i-fix-a-merge-conflict-due-to-removal-of-a-file-in-a-branch) – BuZZ-dEE Oct 05 '15 at 18:08
  • @MarkRushakoff I would rephrase your advice to "when I run into the situation where I need to resolve the conflict I simply don't resolve those for real just mask them so they bite someone (probably not me) in the future". This is really good advice for someone who would like to bring more problems into the project. – Victor Yarema Mar 26 '19 at 12:33

6 Answers6

164

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.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • I had several files, all in the same directory and all with the same suffix. Is there a shortcut to remove them all at once while preserving the rest of the files in the dir? – chiborg Oct 18 '11 at 17:34
  • @chiborg: That's just a shell question. `cd` to the directory, and `git rm *.ext`. Your *shell* (not Git) expands `*.ext` to all matching filenames. – Cascabel Oct 18 '11 at 17:39
  • And if I want to preserve some of the files? `git rm` has no `-i` flag. – chiborg Oct 18 '11 at 18:26
  • @chiborg: You said you wanted to remove everything with a given suffix, and leave everything else intact. That's exactly what I told you how to do. Or did you mean `git rm *-suffix.ext`? Same difference. If you're having a problem figuring out how to use shell wildcards, ask on unix.stackexchange.com. If you know for a fact that what you want can't be done with globbing, use `rm -i` and follow with `git add -u` to pick up the deletions. – Cascabel Oct 18 '11 at 21:45
  • 3
    @Jefromi, if I'm specifying `-s recursive -X ours`, why is it still necessary to `git rm` and `git add`? – Noel Yap May 11 '13 at 18:06
  • This answer seems to suggest that the conflict resolution requires you to simply choose between only two options - either to delete the file or reinstate the file. Many times that is a valid solution. But the correct procedure is to check for what the "modifications" are and suitably reapply them like what @Joseph Ravenwolfe has suggested. For example often deletions are a side effect of renaming files. In that case the one would have to reapply the changes onto the new file + delete the old file. Otherwise, you've just lost some changes in the name of conflict resolution. – Shriram V Feb 11 '22 at 11:47
29

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
Joseph Ravenwolfe
  • 6,480
  • 6
  • 31
  • 31
  • 6
    yes! This is crucial if you don't want to just blow away the changes you're merging, like when I a file was just renamed or its content was moved before the file was deleted. – Edward Anderson Dec 10 '15 at 17:56
  • 9
    As a reminder: This doesn't work when the conflict happens during *rebasing*. Need to be explicit like `git diff mybranch@{1}...origin/master -- path/to/file` – nschum Aug 17 '16 at 08:08
  • @nschum Thanks! This is precisely what I was looking for. – Chuim Sep 26 '16 at 17:56
  • 4
    You can also use `git diff --base` (see my other answer). – Dorian Marchal Aug 27 '19 at 12:50
  • Hi Joseph, I'm still stuck even though this answer looks promising. If you have a moment, I'd love for you to answer here: https://stackoverflow.com/q/63044843/470749 Thanks! – Ryan Jul 23 '20 at 12:25
  • Hi @nschum , I'm still stuck even though this answer looks promising. If you have a moment, I'd love for you to answer here: https://stackoverflow.com/q/63044843/470749 Thanks! – Ryan Jul 23 '20 at 12:25
9

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

6

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.

Dorian Marchal
  • 634
  • 1
  • 9
  • 14
0

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)
borjab
  • 11,149
  • 6
  • 71
  • 98
0

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

Amos Egel
  • 937
  • 10
  • 24