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

时间:2021-01-15 15:12:29

标签: git gitignore

我试图在 .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

1 个答案:

答案 0 :(得分:3)

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

git rm -r <PATH> 它,提交,它应该开始被适当地忽略。

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

相关问题