Search code examples
gitgitignore

Why does Git ignore not work for __pycache__ folder?


I have 3 paths that I'm trying to ignore in my .gitignore file:

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

Everything gets ignored except for 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")

If I comment out the lines in .gitignore, those directories are there again in git status. .gitignore with lines commented out:

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

Result in 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/

Why is only the __pycache__ directory not working with .gitignore?


Solution

  • Notice the modified in your git status output. That means you already added and commited the __pycache__.

    git rm -rf <PATH> it, commit and it should start to be properly ignored.

    It is not a bad idea to start with official .gitignores from the beginning and extend those: https://github.com/github/gitignore