为什么我不能用 .gitignore 排除 __pycache__ 目录?

Why am I not able to exclude __pycache__ directory with .gitignore?

我试图用这些行排除 pycache 目录:

aws_scripts/python/aws_tools/__pycache__/
*.pyc

我可以从当前位置列出该目录:

ls .\aws_scripts\python\aws_tools\__pycache__\

    Directory: C:\Users\bluet\OneDrive\Desktop\important_folders\Jokefire\git\jf_cloud_scripts\aws_scripts\python\aws_tools\__pycache__

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
la---           5/16/2021 10:47 PM            763 aws_add_sg_list.cpython-39.pyc
la---           5/16/2021 10:47 PM           1173 aws_tag_resources.cpython-39.pyc
la---           5/16/2021 10:47 PM            894 banners.cpython-39.pyc
la---           5/16/2021 10:47 PM           1148 choose_accounts.cpython-39.pyc
la---            5/4/2021  8:10 AM          12871 ec2_mongo.cpython-38.pyc
la---           5/15/2021 10:01 AM          15714 ec2_mongo.cpython-39.pyc
la---           5/16/2021 10:47 PM            811 find_vpcs.cpython-39.pyc
la---           5/16/2021 10:47 PM           1183 init.cpython-39.pyc
la---           5/16/2021 10:47 PM            848 list_new_instances.cpython-39.pyc 
la---           5/16/2021 10:47 PM            937 modules.cpython-39.pyc
la---           5/16/2021 10:47 PM            665 read_account_info.cpython-39.pyc  
la---           5/16/2021 10:47 PM           6764 user_input.cpython-39.pyc

然而,当我执行 git 状态时,它仍然显示 __pycache__ 目录:

git status        
On branch develop
Your branch is up to date with 'origin/develop'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    aws_scripts/python/aws_tools/.gitignore

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:   .gitignore
        modified:   aws_scripts/python/aws_tools/__pycache__/ec2_mongo.cpython-39.pyc

我做错了什么?

将其添加到您的 .gitignore 文件中 __pycache__/

编辑:

这是因为您已经将其添加到暂存区。试试这个:

git rm --cached -r __pycache__