【问题标题】: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

标签: git gitignore


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2020-12-10
    • 2015-04-20
    • 2020-10-25
    • 1970-01-01
    • 2015-05-23
    • 2016-04-09
    • 2018-08-31
    • 1970-01-01
    • 2011-04-19
    相关资源
    最近更新 更多