为啥 Git 忽略不适用于 __pycache__ 文件夹? [复制]

Posted

技术标签:

【中文标题】为啥 Git 忽略不适用于 __pycache__ 文件夹? [复制]【英文标题】:Why does Git ignore not work for __pycache__ folder? [duplicate]为什么 Git 忽略不适用于 __pycache__ 文件夹? [复制] 【发布时间】:2021-04-20 15:08:44 【问题描述】:

我的 .gitignore 文件中有 3 条路径试图忽略:

aws_scripts/python/aws_tools/__pycache__/
.vscode/
aws_scripts/output_files/
aws_scripts/source_files/

除了aws_scripts/python/aws_tools/__pycache__/之外的所有内容都会被忽略

git status
On branch develop
Your branch is up to date with 'origin/develop'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   aws_scripts/python/aws_tools/__pycache__/ec2_mongo.cpython-39.pyc

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore

no changes added to commit (use "git add" and/or "git commit -a")

如果我注释掉.gitignore 中的行,这些目录又会出现在git status 中。 .gitignore 注释掉的行:

#aws_scripts/python/aws_tools/__pycache__/
#.vscode/
#aws_scripts/output_files/
#aws_scripts/source_files/

结果为@​​987654329@:

git status
On branch develop
Your branch is up to date with 'origin/develop'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   aws_scripts/python/aws_tools/__pycache__/ec2_mongo.cpython-39.pyc

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        .vscode/
        aws_scripts/output_files/
        aws_scripts/source_files/

为什么只有__pycache__ 目录不能与.gitignore 一起使用?

【问题讨论】:

您不能忽略跟踪的文件。 这些目录未被跟踪:Untracked files: (use "git add &lt;file&gt;..." to include in what will be committed) .gitignore .vscode/ aws_scripts/output_files/ aws_scripts/source_files/ 但是当我将它们添加到.gitignore 并且我执行 git status 时它们消失了。 aws_scripts/python/aws_tools/__pycache__/ec2_mongo.cpython-39.pyc 是一个被跟踪的文件,否则 git 不会将它列为 modified 【参考方案1】:

请注意git status 输出中的modified。这意味着您已经添加并提交了__pycache__

git rm -r &lt;PATH&gt;它,提交,它应该开始被正确忽略。

从一开始就从官方.gitignores 开始并扩展它们是一个不错的主意:https://github.com/github/gitignore

【讨论】:

以上是关于为啥 Git 忽略不适用于 __pycache__ 文件夹? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

小知识点如何在 git 中取消 __pycache__ 文件

git如何清除远程 __pycahce__ 文件

Python的__pycache__到底缓存什么?能缓存历史运行结果吗?—— 对PyCache的一点研究

git使用教程13-.gitignore忽略文件不提交

从 pythonanywhere 添加/提交时,将 `__pycache__` 保留在我的存储库之外

为啥我的窗口不适用于 on_draw?