0

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.

  • 4
    Actually if you delete _foo.txt_ in one branch and edit _foo.txt_ in the other, then when you merge them there will be a _conflict_. So you are not describing correctly what happened. – matt Jan 21 '21 at 18:42

1 Answers1

2

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

Pierre Lezan
  • 120
  • 1
  • 10