Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/65833624/how-to-avoid-adding-deleted-files-in-git-merge

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...