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


当前回答

在我的服务器linux服务器上(在我的本地dev mac上不是真的),只要我不添加星号,目录就会被忽略:

www/档案/*

我不知道为什么,但这让我放松了几个小时,所以我想分享。。。

其他回答

如果您想停止跟踪文件而不从本地系统中删除文件,我更喜欢忽略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 update-index --assume-unchanged path/to/file

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

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

我遇到的另一个问题是我放置了一个内联注释。

tmp/*   # ignore my tmp folder (this doesn't work)

这行得通

# ignore my tmp folder
tmp/

我遵循了这些步骤

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

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

如果.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等”的文件,当然,您没有。