首页 > 解决方案 > 为什么 .gitignore 中的目录仍然被“git status”跟踪?

问题描述

我的 .gitignore 文件包括

project/app/__pycache__

但是当我运行 git status 时,它会报告此目录中文件的更改...

mydomain:main satishp$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)



deleted:    project/app/__pycache__/__init__.cpython-37.pyc
    deleted:    project/app/__pycache__/models.cpython-37.pyc

不应该在 .gitignore 中包含这个目录来防止这种情况发生吗?还是我做错了什么?

标签: gitgitignorestatus

解决方案


这是一种常见的情况:您提交了一些文件,但后来您才意识到您实际上应该忽略它们。

一种解决方案是清除 Git 缓存(改编自Git Tower 的文章):

  1. 用正确的模式更新你.gitignore的(听起来你已经这样做了)。
  2. 提交或存储任何未完成的更改。你的工作副本必须是干净的(即git status必须返回“没有提交,工作树干净”)
  3. git rm与选项一起使用--cached以删除在被忽略之前意外提交的有问题的文件/模式,例如
git rm -r --cached project/app/__pycache__

或指定确切的文件/目录,例如git rm -r --cached path/to/file

  1. 现在您可以将这些更改添加到您的存储库的历史记录中:git add .

  2. 并提交它们:git commit -m "Clean up ignored files"

这应该让你的回购走上正轨。


推荐阅读