6

My .gitignore file consists of

project/app/__pycache__

But when I run git status, it reports changes to files in this directory ...

mydomain:main satishp$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)



deleted:    project/app/__pycache__/__init__.cpython-37.pyc
    deleted:    project/app/__pycache__/models.cpython-37.pyc

Shouldn't the inclusion of this directory in .gitignore prevent this? Or am I doing something else wrong?

Dave
  • 15,639
  • 133
  • 442
  • 830
  • if It was tracked before you added the path to ignore you need to untrack the folder explicity. – Hector Martinez Apr 16 '20 at 02:52
  • Does this answer your question? [Git diff doesn't ignore specified files in .gitignore](https://stackoverflow.com/questions/17820056/git-diff-doesnt-ignore-specified-files-in-gitignore) – matt Apr 16 '20 at 02:55
  • Side note: Git never tracks *directories* at all, only individual files. The `git status` command will sometimes summarize untracked files by listing a directory name, to avoid having to list all the files' names, but tracked-or-untracked-ness is file-by-file. – torek Apr 16 '20 at 06:40
  • Ignore patterns, by contrast, really can ignore entire directories. The interactions are ... tricky. – torek Apr 16 '20 at 06:41
  • 1
    Does this answer your question? [How to make Git "forget" about a file that was tracked but is now in .gitignore?](https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore) – phd Apr 16 '20 at 07:50

3 Answers3

6

This is a common scenario: you committed some files and only later did you realize that you should actually ignore them.

One solution is to clean out the Git cache (adapted from Git Tower's article):

  1. Update your .gitignore with the proper patterns (it sounds like you've already done this).
  2. Commit or stash any outstanding changes. Your working copy must be clean (i.e. git status must return "nothing to commit, working tree clean")
  3. Use git rm with the --cached option to remove the offending files/patterns that were accidentally committed prior to being ignored, e.g.
git rm -r --cached project/app/__pycache__

Or specify the exact files/dirs, e.g. git rm -r --cached path/to/file

  1. Now you can add these changes to your repo's history: git add .

  2. And commit them: git commit -m "Clean up ignored files"

That should get your repo on the right track.

Everett
  • 8,746
  • 5
  • 35
  • 49
1

.gitignore only ignores files that are not part of the repository yet. If you already git added some files, their changes will still be tracked. To remove those files from your repository (but not from your file system) use this command

git rm --cached filename

To remove a folder and all files in the folder recursively:

git rm -r --cached <folder>

You can also use --skip-worktree which is for modified tracked files that the user don't want to commit anymore

git update-index --skip-worktree <file>
hassan
  • 81
  • 4
0

That's because it has already been tracked before you add it to .gitignore. It thus now interprets it as deleted. To fix that you have to remove that file from all previous commits. Here is some guidance to do it https://stackoverflow.com/a/35115769/1812262

michaeldel
  • 2,204
  • 1
  • 13
  • 19