.gitignore 规则不生效

6,748 阅读1分钟

问题:配置了 .gitignore 文件,但在 git add 的时候没有忽略掉。

有两种可能:

  1. .gitignore 文件配置有问题,比如我碰到过一次这个问题:我在 windows 系统下的 idea 中使用了 「Add to gitignore」 这个插件去添加需要被 ignore 的文件夹时候,它会在我的 .gitignore 文件中写入:.idea\ ,实际上应该是:/.idea/
  2. 在你添加新的 ignore 规则前,你曾经已经提交过这个规则对应的文件,这时候你需要先把本地缓存删除(改变成未 track 状态)使用指令:
git rm -r --cached .
git add .
git commit -m 'update .gitignore'