git stop tracking a file — remove from git without deleting
Quick Answer
# Remove the file from git tracking (keeps the file on disk)
git rm --cached config/secrets.json
# Add it to .gitignore so it won't be re-added
echo "config/secrets.json" >> .gitignore
git commit -m "stop tracking secrets.json"
When to use this
You accidentally committed a file (like .env or a config with credentials) and need to remove it from the repository history without deleting the file locally.
Other causes & fixes
Untrack an entire directory
git rm --cached -r node_modules/
echo "node_modules/" >> .gitignore
git commit -m "remove node_modules from tracking"
Remove a sensitive file from all past commits
If credentials were committed, git rm --cached only stops future tracking — the file still exists in history. Use git filter-repo to scrub it completely.
# Install: pip install git-filter-repo
git filter-repo --path config/secrets.json --invert-paths
Related