git - 为什么 .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 中包含这个目录来防止这种情况发生吗?还是我做错了什么?
解决方案
这是一种常见的情况:您提交了一些文件,但后来您才意识到您实际上应该忽略它们。
一种解决方案是清除 Git 缓存(改编自Git Tower 的文章):
- 用正确的模式更新你
.gitignore
的(听起来你已经这样做了)。 - 提交或存储任何未完成的更改。你的工作副本必须是干净的(即
git status
必须返回“没有提交,工作树干净”) git rm
与选项一起使用--cached
以删除在被忽略之前意外提交的有问题的文件/模式,例如
git rm -r --cached project/app/__pycache__
或指定确切的文件/目录,例如git rm -r --cached path/to/file
现在您可以将这些更改添加到您的存储库的历史记录中:
git add .
并提交它们:
git commit -m "Clean up ignored files"
这应该让你的回购走上正轨。
推荐阅读
- c - 如何使用 UIautomation 和纯 C 从浏览器中检索 URL
- javascript - 在 AWS lambda(Nodejs 运行时)中运行 git 命令
- ruby-on-rails - Facebook 登录 URL 被正确的 OAuth 重定向 URI 阻塞
- c# - C# Roles.GetAllRoles() 错误 - 不包含定义
- java - 如何将 Java 线程同步为嵌套类?
- c - 试图读取文件并将其内容保存到结构中,但不起作用
- python - 如何将 gzip.GzipFile 转换为字典?
- sql - 使用 top 和 order by 查询 - 从 MsSql 转换为 Oracle
- r - 为什么在 R 中使用“粘贴”函数时字符串之间有空格?我使用了分隔符
- sql-server - SSAS MDX - 指定计算的度量值