How can I make Git “forget” about a file that was tracked but is now in .gitignore?

Adding a file to .gitignore tells Git to ignore untracked files matching that pattern. But if the file was already committed, .gitignore has no effect on it. Git will keep tracking changes to that file until you explicitly remove it from the index.

Remove a single file from tracking

Use git rm --cached to stop tracking a file without deleting it from your local filesystem:

$ git rm --cached config/database.yml
rm 'config/database.yml'

The file stays on disk, but Git treats it as untracked from this point forward. Your .gitignore rule will now apply to it.

Remove a folder from tracking

For an entire directory, add the -r (recursive) flag:

$ git rm -r --cached logs/
rm 'logs/debug.log'
rm 'logs/error.log'

Everything inside logs/ is removed from the index but remains on your machine.

Remove all tracked-but-ignored files at once

If you have updated .gitignore with several new rules and want to clean everything up in one pass:

$ git rm -r --cached .
$ git add .

The first command removes every file from the index. The second re-adds them, but this time Git respects your .gitignore rules, so any ignored files are left out. Then commit the result:

$ git commit -m "Remove ignored files from tracking"

Preview before removing (dry run)

Not sure what will be affected? Use --dry-run to see what would be removed without actually doing anything:

$ git rm -r --cached --dry-run .
rm 'config/database.yml'
rm 'logs/debug.log'
rm 'logs/error.log'
rm '.env'

Check this list carefully before running the real command.

Warning about collaborators

When you remove a file from tracking and push that change, the file gets deleted for anyone who pulls. Git sees the removal from the index as a deletion.

If the file contains environment-specific configuration (like .env or database credentials), make sure your team knows to back up their local copies before pulling. A heads-up in a commit message goes a long way.

Commit and push

After removing files from tracking, commit and push as normal:

$ git status
Changes to be committed:
  deleted:    config/database.yml
  deleted:    logs/debug.log

$ git commit -m "Stop tracking files now in .gitignore"
$ git push

For more background on ignore rules, see the ignoring files guide. Related commands: git commit, git status, and git add.


DeployHQ deploys only the files Git tracks, so cleaning up your index means cleaner deployments too. Try it free.