How to Make Git Forget a Tracked File After Adding It to .gitignore

Written: August 14, 2026

Adding a path to .gitignore only affects untracked files. If Git already tracks the file, you must remove it from the index while leaving your working copy alone.

Commands

# 1) Add the path to .gitignore
echo 'secrets.env' >> .gitignore

# 2) Untrack but keep the local file
git rm --cached secrets.env

# 3) Commit the ignore + index change
git add .gitignore
git commit -m "Stop tracking secrets.env"

For a whole directory already tracked: git rm -r –cached path/to/dir

Notes

This does not erase the file from earlier commits. If secrets were committed, rotate them and consider history rewriting only with team agreement.

Confirm with git status that the file shows as ignored, not as a delete of your local copy.

Advertisement

Previous Article

Which JSON Content-Type to Use in Node.js

Next Article

Retrieve Clipboard Text on iOS with Appium

Write a Comment

Leave a Comment

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

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨