Why .gitignore isn't working — and why .env still ended up on GitHub

You added .env to .gitignore. You committed. You open the repo on GitHub — and there it is. Sitting there, keys glowing.
First thought: ".gitignore is broken." It isn't. It does exactly what it does, and doesn't do the thing you expected.
Here's the sentence that explains nine cases out of ten:
.gitignoredoesn't hide files. It only stops new ones from being added.
If a file already made it into Git — once, ever, a month ago — ignore rules no longer apply to it. At all. It's family now, and ignoring only works on strangers.
First, one command that names the culprit
Don't guess. Ask Git directly:
git check-ignore -v .env
Three outcomes:
- A line like
.gitignore:3:.env .env— the rule exists and works. So the rule isn't the problem; go to cause 1. - Nothing printed — Git sees no matching rule. Go to cause 2.
- A rule you didn't expect — another file or another line overrode you. Cause 3.
Half a minute, and you know where to look. Now in order.
Cause 1. The file is already tracked
By far the most common. Check it like this:
git ls-files | grep .env
If the name prints, the file is in the index and .gitignore is powerless here.
The fix. Take the file out from under Git's watch while keeping it on disk:
git rm --cached .env
git commit -m "stop tracking .env"
The critical bit is --cached: it strikes the file from Git without deleting it from your machine. Without that flag you wipe the real file and ruin your evening.
If the mess is bigger (say a node_modules folder went up), rebuild the index wholesale:
git rm -r --cached .
git add .
git commit -m "apply gitignore"
Git drops everything from the index and picks it back up — this time with .gitignore in mind. Files on disk are untouched.
Cause 2. The rule isn't written the way you think
check-ignore said nothing, so the pattern doesn't match. The classics:
logsandlogs/differ. The first ignores a file and a folder by that name. The second only a folder.- A leading slash anchors to the root.
/config.jsonmatches only in the repo root.config.jsonmatches at any depth. - A slash in the middle kills recursion.
build/*.logonly coversbuild. Plain*.logcatches logs at every level. - A trailing space. Invisible, and it breaks the match. Genuinely painful: you won't spot it by eye.
The fix. Edit the pattern and re-check with git check-ignore -v after every edit. Not "commit and see what GitHub says" — the command answers instantly.
Cause 3. The rule exists, but another one wins
A project can have more than one .gitignore. Git reads them top down: the root one, then files in subfolders, plus your personal global one (core.excludesFile) and the internal .git/info/exclude. On conflict, the last matching rule wins.
That leaves two traps.
The first is line order. You wrote *.log, then !important.log below — the file comes back into the index. Swap them and it doesn't.
The second is subtler and ruins more plans: you can't re-include a file if the whole folder above it is excluded. This will not work:
secrets/
!secrets/example.env
Git never walks into an excluded folder, so it never learns example.env exists. Exclude the contents, not the folder itself:
secrets/*
!secrets/example.env
The fix. git check-ignore -v prints the rules file and the line number — edit right there. It also reveals when a global ignore you forgot about years ago is overriding you.
If it was .env — fix the key too
Here's the unpleasant part. git rm --cached removes the file from future commits, but history stays history. A key that once went to a public repo sits in an old commit and opens with a direct link. Bots scanning GitHub for fresh keys find that within minutes.
So the order of operations is:
- Revoke and reissue the key. Right now, before anything else. The old one must stop working.
- Remove the file from the index (
git rm --cached .env). - Put
.envin.gitignoreand commit. - Add a
.env.exampleto the repo — variable names only, no values.
Rewriting history is possible, but it's a separate operation with consequences for everyone who cloned the repo — and it still doesn't cancel step 1. More on storage in how to store API keys safely, and on why environment variables exist at all, right next door.
Why does my AI agent keep committing junk?
Because it runs git add ., and that . sweeps up everything Git considers its own — including already-tracked files that are "supposedly ignored". The fix isn't a prompt, it's the state of the repo: untrack the junk once, and the agent physically can't add it again.
Should I commit the .gitignore file itself?
Yes, always. .gitignore is part of the project: it has to travel with the code to anyone who clones the repo, or your teammate simply won't have the rule. Personal exclusions that don't concern the team (your editor's files, local scratch notes) belong in the global ignore or .git/info/exclude — those never leave your machine.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





