Why Is .gitignore Not Working? How to Fix It
gitignore only affects untracked files — if a file is already committed, adding it to .gitignore won't stop Git from tracking it. Learn how to fix it.
Published September 20, 2026
A .gitignore only affects untracked files. If Git already tracks a file, ignoring it has no effect until you remove it from the index with git rm --cached.
The concept
Git tracks files by adding them to its index. .gitignore tells Git not to start tracking new files, but it does not untrack files that were committed earlier.
Other causes: the pattern is wrong, the file is in a nested folder, the file has the wrong name or encoding, or a global ignore overrides it.
Stop tracking an already committed file
echo '.env' >> .gitignore
git rm --cached .env
rm '.env' (file stays on disk)
git commit -m 'Stop tracking .env'
Practical examples
Find why a file is ignored (or not)
git check-ignore -v path/to/fileUntrack a folder
git rm -r --cached node_modules
git commit -m "Untrack node_modules"Typical .gitignore patterns
node_modules/
.env
*.log
/dist
!keep.logReset the whole index
git rm -r --cached .
git add .
git commit -m "Apply .gitignore"Common mistakes
- Expecting .gitignore to untrack committed files.
- Adding a trailing space or wrong slash in a pattern.
- Ignoring a secret after it was committed — rotate the secret, the history still has it.
Key takeaways
- Ignore only affects untracked files.
- git rm --cached stops tracking without deleting.
- git check-ignore -v explains matches.
FAQ
Do I need to commit .gitignore?
Yes, so everyone shares the same rules.
How do I ignore a file only on my machine?
Add it to .git/info/exclude or your global ignore file.