了解如何处理 Git 中的文件删除

Understanding how to deal with file deletions in Git

我最近在 git 上遇到了一个问题,当时我的一个推送在 origin/master 上被还原了,但我留在我的分支中继续我的功能,直到它准备好进行最后的推送。当我尝试 git pull --rebase 时,我的一些本地更改由于还原而被完全删除。但是,通过拉合并,我能够推送在 origin/master 上删除的本地文件,因为它触发了冲突。

我试图用本地分支重现这个,但没有成功。我在分支 develop 上创建了文件 a,将其合并到 master,从 master 删除了文件 a,在 [=15] 上添加了文件 b =],并尝试将 master 合并到 develop.

此时我期待合并在 master 的已删除文件 a 和 develop 的现有文件 a 之间产生冲突,但它只是删除了 develop 的文件a 就像我的 pull --rebase 在我原来的问题中所做的那样。为什么?

这是常见行为,已在 the docs 中描述。

首先,为什么您的复制没有产生合并冲突

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.

同段中的下一句描述了为什么在没有 --rebase 的情况下将 origin 拉到您的主服务器会造成合并冲突:

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.

合并冲突 - 在您的情况下 - 不会创建,因为只有合并在一起的双方之一影响了该文件。如果您在将 master 合并到该分支之前更改了 develop 中的文件 a,它会像您的 pull 那样产生合并冲突。您在那里更改了在 origin 上删除的文件,这会产生合并冲突。