为什么Git忽略 __pycache__ 文件夹无效?

19

我有三个路径在我的.gitignore文件中想要忽略:

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一起工作?


4
不能忽略已跟踪的文件。 - tkausl
这些目录未被跟踪:未跟踪的文件: (使用“git add <file>…”将其包含在提交中) .gitignore .vscode/ aws_scripts/output_files/ aws_scripts/source_files/但是当我将它们添加到.gitignore并执行git status时,它们就会消失。 - Tim Dunphy
2
aws_scripts/python/aws_tools/__pycache__/ec2_mongo.cpython-39.pyc 是一个被追踪的文件,否则 Git 不会将其列为“已修改”。 - tkausl
1个回答

37

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

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

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


1
我不得不添加一个-f来强制它工作,但它起了作用。谢谢 git rm -r -f pycache - Malcolm Anderson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接