如何防止git提交删除文件

How to prevent git commit from deleting files

我刚刚做了一个 git 提交,它似乎随机地从我的 node_modules 文件夹中删除了一些文件。有人遇到过这个问题吗?

需要说明的是,我最近的 git 提交已删除

delete mode 100644 node_modules/grunt-google-cdn/node_modules/google-cdn/node_modules/cdnjs-cdn-data/external/cdnjs.json

 delete mode 100644 node_modules/grunt-node-inspector/node_modules/node-inspector/front-end-node/Images/src/favicon.eps

 delete mode 100644 node_modules/moment/min/tests.js

 delete mode 100755 node_modules/requirejs/bin/r.js

我以前从未见过这种情况,我无法开始理解为什么会这样。这使我无法构建用于部署的应用程序。有谁知道 how/why 发生过这种情况以及将来如何避免这种情况?

我的预期是这些文件在某个时候被删除了(通过什么机制,我不能说)。

如果您使用 Git 2.0 或更高版本,当您键入 git add . 时,您暂存了已删除的文件以供提交。 git add . 基本上表示 "I want to stage all of the changes in this repo",在这种情况下包括删除文件。

该行为在之前的版本中有所不同(我刚刚针对 Git 1.9 和 2.6.3 验证了不同的行为)。

I only did git add . beforehand

因此您要求 Git 暂存 .(当前目录)中的所有内容。这个includes deleted files:

e.g. specifying dir will record not just a file dir/file1 modified in the working tree, a file dir/file2 added to the working tree, but also a file dir/file3 removed from the working tree

如果这些文件被某些非 git 进程删除并且您随后 运行 git add . 然后提交,则删除将被提交。

比较 git-addversion 1.9.2 and version 2.0.0 的文档表明此行为在版本 2 中发生了变化。

告诉git忽略提交中删除的文件并提交所有其他更改:

git add --ignore-removal .