38

I see that with python 3, there is a __pycache__ in each subfolder of my application and of course *.pyc files will be created there as well. In my .gitignore of my app's root folder, can I simply place:

**/__pycache__
**/*.pyc

to have these ignored in all future subfolders created? Or do I need to place a .gitignore in each and every subfolder ?

On a related note, how do I check what all is being untracked (ignored). I tried "git status -u" and it does not show __pycache__ or .pyc files as being untracked.

phd
  • 82,685
  • 13
  • 120
  • 165
G.A.
  • 1,443
  • 2
  • 22
  • 34
  • 2
    To see if a file is ignored, and if so, why, use `git check-ignore -v`. Note that a file that is already in the index is tracked, regardless of whether it might match a `.gitignore` directive; see the `--no-index` option as well. The `git check-ignore` command first appeared in Git version 1.8.2. – torek Jul 30 '18 at 05:00

1 Answers1

77

You should not need the **/:

 __pycache__/
 *.pyc

That should be enough.

See for instance gitignore.io/python.
Note the practice of adding a trailing / to ignore specifically a folder.

Use git check-ignore -v -- afile (that I mentioned in Sept. 2013, with Git 1.8.2+) to check which rule ignores a file.
If the folder was already tracked:

git rm --cached -r __pycache__/

André Duarte adds in the comments:

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

git status | grep pycache | awk '{print $3}' | xargs git reset HEAD
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • After adjust gitignore, I had to do this for every file, so I call awk for help: "git status | grep pycache | awk {'print $3'} | xargs git reset HEAD" – André Duarte Oct 04 '19 at 14:24
  • I still have those __pycache__ under the subfolders exist. not sure why? And when I run git check-ignore -v, I got an error fatal: no path specified. – GLP Dec 18 '21 at 06:31
  • @GLP `git check-ignore -v` is supposed to be followed by a path: `git check-ignore -v -- path/to/pycache/a/file`: that way, you can check if a .gitignore rule applies to any file in a `pycache` folder. – VonC Dec 18 '21 at 13:48