如何从 git 中删除文件夹和文件?

How to remove folders and files from git?

我有一些文件夹包含在过去的 git 提交中,现在我想将其从 git 中删除。

所以我在.git中添加了文件夹。git忽略。但是,如果我尝试从服务器中提取更改,则会抛出一个错误:

Pull failed. Some untracked working tree files would be overwritten by Pull. Please move or remove them before you can Pull.

在.git忽略是行

/storage 

应该忽略存储目录中的所有内容。那么,为什么拉取 storage/logs/20201455.log 文件中的文件有问题?

如果存储目录已经签入到您的存储库中,您需要将其删除:

git rm -r storage
git commit -m "Removed storage"

但这也会删除您的存储目录的内容,因此请先将其移到安全的地方。

mv storage storage_orig
git rm -r storage
git commit -m "Removed storage"
mv storage_orig storage

除了 biomiker 提到的,你还可以这样做:

git rm -r --cached storage

这只会将其从存储库中删除并保留文件,请记住,您必须将 ./storage 添加到 .gitignore 文件中,以确保它不会再次添加到存储库中。