Dave
Dave

Reputation: 19330

Why is a directory in .gitignore still getting tracked by "git status"?

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?

Upvotes: 7

Views: 4297

Answers (3)

hassan
hassan

Reputation: 176

.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>

Upvotes: 3

Everett
Everett

Reputation: 9638

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.

Upvotes: 7

michaeldel
michaeldel

Reputation: 2385

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

Upvotes: 0

Related Questions