Updated to address follow-up questions in the comments
.gitignore
file only works on untracked files.
If paths show up as "modified" or "unstaged", that means the files are already tracked - they were in the index and probably in prior commits before you put them in gitignore, or they were force added after you put them in gitignore.
So you're trying to ignore modifications to tracked files, and that is not something you can do in git.
(To be clear, you will likely find answers saying that you can use various flags on the index to ignore changes to staged files. That almost always leads to trouble, because it's not what those flags are for.)
So how can you get these files ignored? You have to tell git to stop tracking them. This is easy to do, but it has potentially troublesome side effects.
Assuming you don't have a lot of long-lived branches, the simple approach is git rm --cached
to unstage the changes, and then create a new commit. For example if you're only worried about cleaning up the master branch, you could
git checkout master
git rm -r --cached __pycache__ # or whatever other paths
git commit -m "remove unwanted files'
Now you should find that git status
does not show these files by default, and git add
does not add them. BUT, if you do something that changes the files and then
git checkout HEAD^
moving back to a commit that had these files in it, git will quietly clobber the changes you had made to those files even though it doesn't have a copy of the changes from which to restore. And when you subsequently
git checkout master
they will be deleted from the work-tree entirely.
For *.pyc
files, maybe that's acceptable (since Python quickly/quietly regenerates them anyway); for other files it may not be, so this is at best a 'use with caution' approach.
An alternative - which avoids the above problems but comes with its own costs - is to rewrite history to look like the ignored files were never committed.
Be aware that, as with any history rewrite, you would have to coordinate with anyone else that shares the repo.
If you decide to follow this path, there are existing questions/answers that document the procedure; you'd be looking for git-filter-branch with either a tree-filter or an index-filter.