在已经跟踪大量文件的现有存储库上应用.gitignore [英] Apply .gitignore on an existing repository already tracking large number of files
问题描述
我的存储库中有一个现有的Visual Studio项目.我最近在我的项目下添加了一个.gitignore文件,并且我认为这告诉Git忽略该文件中列出的文件.
I have an existing Visual Studio project in my repository. I recently added a .gitignore file under my project and I assume that tells Git to ignore the files listed in the file.
我的问题是所有这些文件已经被跟踪,据我所知,Git不会忽略在将规则添加到该文件之前已经被跟踪的文件.
My problem is that all those files are already being tracked and as far as I know Git will not ignore a file that was already tracked before a rule was added to this file to ignore it.
建议使用:git rm --cached
并手动取消跟踪它们,但这将使我永远永远一个接一个地进行操作.
It was suggested to use: git rm --cached
and manually un-track them but that's going to take me forever to go through them one by one.
我考虑过删除存储库,然后重新创建它,但是这次存在.gitignore文件,但是必须有一种更好的方法.
I thought about deleting the repository and recreating it again but this time with .gitignore file present, but there must be a better way to do this.
推荐答案
此 answer 解决了我的问题:
首先,提交所有待处理的更改.
然后运行以下命令:
git rm -r --cached .
这将从索引中删除所有内容,然后运行:
This removes everything from the index, then just run:
git add .
提交:
git commit -m ".gitignore is now working"
这篇关于在已经跟踪大量文件的现有存储库上应用.gitignore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!