.gitignore Not Working: Stop Tracking a File Git Already Has

If a file is still showing up in git status after you added it to .gitignore, it is almost always because Git was already tracking it. .gitignore only affects files Git has not seen yet. To stop tracking a file but keep it on disk, run:

Bash
git rm --cached settings.json
git commit -m "Stop tracking settings.json"

Change settings.json to your file. --cached removes it from Git only; the file stays in your folder. For a whole folder, add -r:

Bash
git rm -r --cached logs
PowerShell: settings.json committed, the .gitignore rule ignored while it is tracked, git rm --cached and a commit, then git check-ignore showing .gitignore:1:settings.json

Check That It Is Tracked

Bash
# is the file in Git's index?
git ls-files settings.json

# which .gitignore rule matches it (nothing is printed for a tracked file)
git check-ignore -v settings.json

If git ls-files prints the name, the file is tracked and the ignore rule is being skipped. git check-ignore -v prints nothing for a tracked file. After the git rm --cached commit it shows the rule and line number, for example .gitignore:1:settings.json. That is also the quickest way to see which rule is, or is not, matching.

Other Reasons a Rule Does Nothing

  • The rule has a comment on the same line. a.tmp # note does not ignore a.tmp; a comment must be on its own line starting with #.
  • A folder is ignored and you try to un-ignore a file inside it. logs/ followed by !logs/keep.txt does not work, because Git never looks inside an ignored folder. Ignore logs/* instead of logs/.
  • The file is in the wrong place or has the wrong name. It must be called exactly .gitignore and sit in the repository (or in a subfolder, for rules that apply there).
  • The file is in earlier commits and you need it gone from history, not just from now on. git rm --cached stops tracking from this commit forward; the old commits still contain it.

Why It Happens

Git decides what to track by what is in its index, and .gitignore is only a list of files not yet in the index that it should leave alone. Once a file has been added, Git carries on watching it whatever the ignore file says. Removing it from the index with git rm --cached hands it back to .gitignore.

If the file held a password or API key, ignoring it is not enough. Anyone with the repository can still read it in the old commits, so change the key as well. The Claude Code posts on CLAUDE.md and permissions suggest ignoring local settings files, and this is the fix when one was committed first.

Common Questions

Will git rm –cached delete my file?

No. --cached limits it to Git’s index. Without --cached, git rm deletes the file from disk too.

How do I stop tracking everything that should be ignored?

Run git rm -r --cached ., then git add . and commit. Everything is re-added, and the ignored files are the ones that no longer come back. Check git status before you commit.

Will my teammates lose the file?

When they pull your commit, Git removes the file from their folder too, because the commit deletes it from the repository. Tell them to keep a copy first if it holds local settings.

Related

If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *