如何删除.gitignore中列出但仍在存储库中的文件? [英] How to remove files that are listed in the .gitignore but still on the repository?
问题描述
我的存储库中有一些文件应该被忽略,我将它们添加到.gitignore中,但当然,它们不会从我的存储库中删除。
I have some files in my repository that should be ignored, i added them to the .gitignore but, of course, they are not removed from my repository.
所以我的问题是,是否有一个使用filter-branch的魔术命令或脚本,可以重写我的历史记录并轻松删除所有这些文件?或者只是一个会创建一个提交将删除它们的命令?
So my question is, is there a magic command or script using filter-branch that can rewrite my history and remove all these files easily? Or simply a command that will create a commit that will remove them ?
推荐答案
您可以手动将它们从存储库中删除:
You can remove them from the repository manually:
git rm --cached file1 file2 dir/file3
或者,如果你有很多文件:
Or, if you have a lot of files:
git rm --cached `git ls-files -i --exclude-from=.gitignore`
但这并不似乎在Windows上的Git Bash中工作。它产生一个错误信息。以下工作更好:
But this doesn't seem to work in Git Bash on Windows. It produces an error message. The following works better:
git ls-files -i --exclude-from=.gitignore | xargs git rm --cached
关于重写没有这些文件的整个历史记录,我非常怀疑有一个自动的方式来做到这一点。
我们都知道重写历史很糟糕,不是吗? :)
Regarding rewriting the whole history without these files, I highly doubt there's an automatic way to do it.
And we all know that rewriting the history is bad, don't we? :)
这篇关于如何删除.gitignore中列出但仍在存储库中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!