How to avoid adding deleted files in git merge?

Let's say there are 2 git branches: master and dev. In dev I deleted foo.txt. After that in master some changes are added to foo.txt. When I merge master to dev foo.txt is created although it was deleted from dev.

So is there a way to NOT recreate deleted files on git merge?

EDIT: matt pointed out that it will cause conflict and simply add the deleted file again. So the question is rather how to exclude the files from merge that have no counterpart in the target branch.

Upvotes: 1

Views: 1194

Answers (1)

Pierre Lezan
Pierre Lezan

Reputation: 171

If i've understood your issue properly here is what the git log should looks like :

* ff3f8a1 (HEAD -> master) changed text
| * 8a4d7d7 (dev) removed file
|/  
* 615d151 initial commit

If you do git merge dev to merge branch dev onto master, you will end up with a merge conflict. That's why the file came back. You have to resolve that conflict manualy by either:

  • removing the file (git rm --force <file>)
  • getting the remote version (on the deleted side) of it (git checkout --theirs <file>)

git merge --strategy-option=theirs won't be able handle those kind of conflicts (modify/delete)

AFAIK there is no proper way arround that without manual conflict resolution, but there are some hacky solutions like this one wich deletes all the files that fail to merge (can be dangerous): https://stackoverflow.com/a/54232519/8541886

Upvotes: 2

Related Questions