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.