【问题标题】:Why is a directory in .gitignore still getting tracked by "git status"?为什么 .gitignore 中的目录仍然被“git status”跟踪?
【发布时间】:2020-04-16 11:07:32
【问题描述】:

我的 .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 gitignore status


【解决方案1】:

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

一种解决方案是清除 Git 缓存(改编自 Git Tower's article):

  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"

这应该让你的 repo 走上正轨。

【讨论】:

    【解决方案2】:

    那是因为在您将其添加到.gitignore 之前已经对其进行了跟踪。因此,它现在将其解释为已删除。要解决这个问题,您必须从所有以前的提交中删除该文件。这里有一些指导https://stackoverflow.com/a/35115769/1812262

    【讨论】:

      猜你喜欢
      • 2021-02-24
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 2021-04-08
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多