为什么 Git 忽略对 __pycache__ 文件夹不起作用?

Why does Git ignore not work for __pycache__ folder?

我在 .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/

结果git status

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 一起使用?

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

git rm -r <PATH> 它,提交,它应该开始被正确忽略。

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