Git Ignore ... and Untrack!
We’ve all done it. You commit a file you shouldn’t have. A .env, a node_modules/ you forgot to ignore, some local config, a stray build folder. So you add it to .gitignore… and git keeps tracking it anyway.
That’s because .gitignore only governs untracked files. Once git is already tracking something, ignoring it does nothing. 🤦♂️ The actual fix is two steps:
git rm --cached path/to/file
echo "/path/to/file" >> .gitignore
I do this often enough (and yet I always have to look it up) that I finally built a VS Code extension to do it for me. (Claude helped)
Ignore & Untrack
git-ignore-and-untrack adds a single Ignore & Untrack action to the Source Control context menu. Right-click a file under Changes, pick it, and in one step it:
- Removes the file from the git index (
git rm --cached), keeping your copy on disk, and - Adds an anchored entry (
/path/to/file) to your.gitignore.
Commit to finalize, and the file is gone from version control while your local copy stays exactly where it is.
It’s on the VS Code Marketplace, the source is on GitHub, and it’s licensed 0BSD — do whatever you want with it.
If you’ve ever committed a secret and scrambled to un-commit it, this one’s for you.
**TBF, this won’t fix your history. Rotate those keys!