我有一个已经初始化的Git存储库,我添加了一个.gitignore文件。我如何刷新文件索引,以便忽略我想要忽略的文件?


当前回答

如果您想停止跟踪文件而不从本地系统中删除文件,我更喜欢忽略config/database.yml文件。只需尝试:

git rm --cached config/database.yml
# this will delete your file from git history but not from your local system.

现在,将此文件添加到.gitignore文件并提交更改。从现在起,git不会跟踪对config/database.yml所做的任何更改。

$ echo config/database.yml >> .gitignore

谢谢

其他回答

如果您想停止跟踪文件而不从本地系统中删除文件,我更喜欢忽略config/database.yml文件。只需尝试:

git rm --cached config/database.yml
# this will delete your file from git history but not from your local system.

现在,将此文件添加到.gitignore文件并提交更改。从现在起,git不会跟踪对config/database.yml所做的任何更改。

$ echo config/database.yml >> .gitignore

谢谢

我遵循了这些步骤

git rm -r --cached .
git add .
git reset HEAD

之后,git删除所有应该忽略的文件(在我的例子中是*.swp)。

要从跟踪中删除几个特定文件,请执行以下操作:

git update-index --assume-unchanged path/to/file

如果您想再次开始跟踪它:

git update-index --no-assume-unchanged path/to/file                      

如果需要停止跟踪大量被忽略的文件,可以组合一些命令:

git ls files-i--排除标准|xargs-L1 git rm--缓存

这将停止跟踪被忽略的文件。如果您想从文件系统中删除文件,请不要使用--cached选项。您还可以指定文件夹来限制搜索,例如:

git-ls文件-i--排除标准--${FOLDER}| xargs-L1 git rm

如果.gitignore似乎没有忽略未跟踪的文件,还需要记住的一点是,您不应该在忽略的行上有注释。所以这没关系

# ignore all foo.txt, foo.markdown, foo.dat, etc.
foo*

但这行不通:

foo*   # ignore all foo.txt, foo.markdown, foo.dat, etc.

.gitignore将后一种情况解释为“忽略名为“foo*#忽略所有foo.txt、foo.markdown、foo.dat等”的文件,当然,您没有。