You added a .gitignore but Git is still tracking files that should be ignored. Why, and how do you fix it?
Take a moment.
Say your answer out loud or write it down first — retrieval is what makes it stick. Reveal when you're ready.
Short answer
.gitignore only affects untracked files — files already committed stay tracked. To stop tracking them, remove them from the index with git rm --cached <file> (keeps the file on disk), commit the removal, and then .gitignore prevents re-adding. Committed secrets need rotation, not just a gitignore update.
Detailed explanation
Git ignore rules apply exclusively to untracked paths. Once a file is staged and committed, adding a pattern to .gitignore has no effect on it — it is already in the index and history. The fix: git rm --cached removes the file from the index without deleting it locally, so your app keeps working while Git forgets about it. Commit that change so the removal propagates; the .gitignore pattern then blocks it from being re-added. For directories, git rm -r --cached <dir>. Per-developer noise (OS files, editor config) belongs in a global ~/.gitignore or .git/info/exclude rather than the repo .gitignore, which is for project-wide ignores. Critical caveat: if the file contained a secret, removing it from Git is not enough — it is still in history. Rotate the secret and consider git filter-repo or BFG to purge history entirely.
Example
# Stop tracking .env (keep locally):
git rm --cached .env
git commit -m 'chore: stop tracking .env'
# Global ignore for personal noise:
git config --global core.excludesfile ~/.gitignore
# ~/.gitignore:
.idea/
.DS_Store
*.logInterview tip
State the untracked-only rule crisply, show rm --cached, then land on the secrets-in-history caveat — that escalation shows you have felt this pain, not just read the docs.
Common mistakes
- Assuming .gitignore retroactively untracks committed files.
- Using
git rmwithout --cached and deleting the local file. - Forgetting to rotate secrets that were committed before being ignored.