在子文件夹中使用 .gitignore 和 *.pyc

Using .gitignore with *.pyc in subfolders

我看到 python 3,在我的应用程序的每个子文件夹中都有一个 __pycache__,当然也会在那里创建 *.pyc 文件。在我的应用程序根文件夹的 .gitignore 中,我可以简单地放置:

**/__pycache__
**/*.pyc

在以后创建的所有子文件夹中忽略这些?或者我需要在每个子文件夹中放置一个 .gitignore 吗?

在相关说明中,我如何检查所有未跟踪(忽略)的内容。我试过 "git status -u" 但它没有显示 __pycache__ 或 .pyc 文件未被跟踪。

你不应该需要 **/:

 __pycache__/
 *.pyc

应该够了。

例如见gitignore.io/python.
请注意添加尾随 / 以专门忽略文件夹的做法。

使用git check-ignore -v -- afile(即I mentioned in Sept. 2013, with Git 1.8.2+)检查忽略文件的规则。
如果文件夹已被跟踪:

git rm --cached -r __pycache__/

André Duarte adds :

After adjusting gitignore, I had to do this for every file, so I call awk for help:

git status | grep pycache | awk '{print }' | xargs git reset HEAD