Git: 如何删除现在在 gitignore 中但之前添加到 repo 中的文件

Git: How to remove files that are now in gitignore but were added to repo before

我有几个文件在 repo 和 .gitignore 中。

我希望这些文件在我执行 git pull origin master

时从存储库中删除,但不从我的生产服务器中删除

我已经尝试了多种解决方案,但 none 接缝工作,在每个解决方案中,我最终在拉取时从服务器中删除了文件。

参考文献:

How to make Git "forget" about a file that was tracked but is now in .gitignore?

Remove directory from remote repository after adding them to .gitignore

How to remove files that are listed in the .gitignore but still on the repository?

据我所知,无法使用 "stop tracking this but keep existing version" 标记来标记文件路径。

部署包含删除文件的第一个版本时,您需要执行一次手动操作。

类似于:

cp file file.prod
git pull
mv file.prod file

你首先需要 git filter-branch,然后要求每个人 rebase 根据你的旧历史创建的分支。

参考:https://help.github.com/articles/remove-sensitive-data/

这很复杂,可能会引起问题,所以下次要小心,不要到达那里。

如果你因为某些原因不能这样做,那就需要等到时光机发明了:)