Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .gitignore with *.pyc in subfolders

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.

like image 793
G.A. Avatar asked Jul 30 '18 04:07

G.A.


People also ask

Does Gitignore work in subfolders?

gitignore file is usually placed in the repository's root directory. However, you can create multiple . gitignore files in different subdirectories in your repository.

Can you Gitignore a folder?

A . gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .


1 Answers

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 
like image 55
VonC Avatar answered Sep 18 '22 13:09

VonC