Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git does not apply deleted files when merging an old branch into the master. How can I tell Git to apply deleted files?

Tags:

git

git-merge

I have a master and a dev branch. I made commits in both. I also deleted some files in dev. I made other commit in master, so this master branch is more recent.

My issue is that merging dev into master dont delete files.

Git consider that these files exists in master and consequently, keep them during the merge.

How can I merge these two branches and force Git to apply all and every commits made in dev ? including deleted files.

Thks for your help!

like image 812
Matthieu Avatar asked May 09 '16 17:05

Matthieu


2 Answers

The only way I can fathom this possible situation is if you created two different files, each with the same filename, in independent branches.

i.e. let's say that the master and dev branches already exist.

  1. create and commit file.txt to master
  2. checkout dev, then again create and commit file.txt to dev. Now because you have created two distinct files, git views them as two separate entities despite the same filename, defeating the whole purpose of version control.
  3. later delete file.txt from dev
  4. merge dev into master, and low and behold file.txt still exists in master, and this makes sense because like I said, git views the two files as completely independent.

notice if you had not deleted file.txt from dev and attempted a merge, then you would have gotten a merge conflict because git wouldn't know how to handle two different entities with the same path/filename.

If this is your scenario, then I'm going to risk arrogance and say you're doing it wrong ;)

The point of a version control system is to let the tool manage your differences between a file at different stages in time as well as the relationship of those changes to other files in the repository.

My suggestion to improve the workflow in this situation would be to checkout the specific file from the other branch:

  1. create and commit file.txt to master
  2. checkout dev, then just grab the particular file from the other branch

    git checkout master -- file.txt
    

    In this situation, you will still be on the dev branch, but have now added file.txt from the master branch.

  3. now git recognizes that these are the same entity. so you can delete the file and commit the removal in dev

  4. merging dev into master will now delete file.txt from master
like image 153
Jeff Puckett Avatar answered Nov 18 '22 07:11

Jeff Puckett


I had the same problem. In my case I think the problem was that, when I did the merge, my copy of the merged-from branch was outdated compared to the remote. (a colleague had done the deletion, not me)

In any case, what fixed it was deleting the whole working copy and cloning it anew.

like image 2
Stefan Monov Avatar answered Nov 18 '22 05:11

Stefan Monov