0%

Git 添加gitignore后不起作用

gitignore - Specifies intentionally untracked files to ignore

.gitignore官方文档

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected
To stop tracking a file that is currently tracked, use git rm --cached to remove the file from the index. The filename can then be added to the .gitignore file to stop the file from being reintroduced in later commits.

原因

.gitignore文件只会作用于未跟踪(untracked)的文件,如果需要作用于已经跟踪(tracked)的文件,加入ignore文件时需要先停止跟踪(stop tracking)从暂存中删除才会生效

解决方法

1
2
3
git rm -r --cached .
git add .
git commit -m "update .gitignore"