3

I know there are many, many posts on this topic. I've tried all of the answers I can find and none of them seem to be addressing my problem.

I started a git project where unfortunately .pyc files in __pycache__ directories were initially being tracked. I realised the problem and then put *.pyc and __pycache__ in .gitignore, but of course they were still being tracked.

I then:

  • Tried to remove the tracked files:

    git rm -r --cached .
    git add .
    git commit -m "fixed untracked files" 
    
  • Checked my .gitignore. The encoding, according to file -I .gitignore, is .gitignore: text/plain; charset=us-ascii. The .gitignore itself is:

    __pycache__
    *.pyc
    buildSite/.DS_Store
    *.DS_Store
    .idea/
    .idea/workspace.xml
    scratchFiles
    

    There aren't any extra whitespaces or anything.

I'm still getting .pyc files showing changes:

git status
...
modified:   buildSite/builder/templatetags/__pycache__/class_tag.cpython-36.pyc

Very grateful for any suggestions!

user3757897
  • 316
  • 2
  • 13
  • did you commit your .gitignore file before running `git rm -r --cached .`? – Jonathan Lam Apr 20 '18 at 16:49
  • Yes - is that a problem? – user3757897 Apr 20 '18 at 16:51
  • I thought it might be a problem if you hadn't committed it, but never mind now – Jonathan Lam Apr 20 '18 at 16:52
  • Try `git ls-files -i -z --exclude-from=.gitignore | xargs -0 git rm --cached` as suggested in [How to remove files that are listed in the .gitignore but still on the repository?](https://stackoverflow.com/questions/13541615/how-to-remove-files-that-are-listed-in-the-gitignore-but-still-on-the-repositor); then `git commit -m "Fix untracked files"` – ErikMD Apr 20 '18 at 17:11
  • 1
    `.gitignore` works even if not committed — `git` consults files, not commits. It would be interesting to see `git status` after `git rm` and `git add`. – phd Apr 20 '18 at 18:01
  • 1
    Also check for simple PEBCAK: was `.` the right directory when you ran `git rm -r --cached .`? – torek Apr 20 '18 at 18:54
  • I had whitespaces before __pycache__ and *.pyc, which caused it to not ignore *.pyc and __pycache__ directories. Removing the whitespace fixed the issue. – NNN Jul 12 '20 at 09:03

1 Answers1

4

Make sure:

  • there is no crlf in your .gitignore file
  • there is no nested git repo (.git/ subolder) in any parent folder of your files to be ignored
  • there is a trailing / when ignoring a folder: __pycache__/, not __pycache__.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This was it, thanks! In messing with my `.gitignore` trying to figure out what was going on, I deleted the trailing `/` before running `git rm - r --cached .`. – user3757897 Apr 23 '18 at 08:45