geranimo
geranimo

Reputation: 366

Understanding how to deal with file deletions in Git

I recently got a problem with git when one of my pushes got reverted on origin/master, but I stayed in my branch to continue my feature until it was ready for the final push. When I tried to git pull --rebase, some of my local changes were completely deleted due to the revert. However with pull merge I was able to push my local files that were deleted on origin/master because it triggered a conflict.

I tried to reproduce this with local branches, but was unsuccessful. I created file a on branch develop, merged it into master, deleted file a from master, added file b on develop, and tried to merge master into develop.

At this point I was expecting the merge to generate a conflict between the master's deleted file a and develop's existing file a, but it just deleted the develop's file a just as my pull --rebase did in my original problem. Why?

Upvotes: 3

Views: 47

Answers (1)

Clijsters
Clijsters

Reputation: 4256

This is common behavior and already described in the docs.

First, why did your reproduction not create merge conflicts?

During a merge, the working tree files are updated to reflect the result of the merge. Among the changes made to the common ancestor’s version, non-overlapping ones (that is, you changed an area of the file while the other side left that area intact, or vice versa) are incorporated in the final result verbatim.

The next sentence in the same paragraph describes why a merge conflict was created by pulling origin without --rebase to your master:

When both sides made changes to the same area, however, Git cannot randomly pick one side over the other, and asks you to resolve it by leaving what both sides did to that area.

A merge conflict - in your case - is not created, as only one of the both sides which are merged together affected that file. If you had changed the file a in develop prior to merging master onto that branch, it would have created a merge conflict, as your pull did. There you changed the file which was deleted on origin and this creates a merge conflict.

Upvotes: 3

Related Questions