Gitignore not working — the rule doesn't reach the index
.gitignore "not working" almost always means the file is already tracked — immune to any rule once in the index. git check-ignore -v decides: a printed rule = rule problem; silence = tracked.
Gitignore not working almost always means the file is already tracked — a file in the index is immune to any rule you write. git check-ignore -v <path> decides it: a printed rule means a rule problem; silence means tracked and no rule applies.
Why is my .gitignore not working?
One command, two outcomes. The silence is the diagnostic.
$ git check-ignore -v debug.log.gitignore:3:*.log debug.log$ git check-ignore -v src/.env$ # (nothing — no rule applies)
| Symptom | Real cause | Fix |
|---|---|---|
Ignored file still shows in git status | Committed before the rule — in the index | git rm --cached + commit |
git add . keeps re-adding it | Same: tracking beats ignoring | Bulk: git rm -r --cached . → git add . |
!build/keep.me never rescues the file | Parent dir excluded — git skips it wholesale | build/* + !build/keep.me |
| A later rule overrides yours | Last matching rule wins | Reorder, or read the winner via check-ignore -v |
| Rule works for teammates, not you (or reverse) | Nested .gitignore / exclude / global mismatch | Match the scope table below |
| VS Code shows it modified after ignoring | Tracked — or a stale SCM view | git rm --cached; reload window first |
| Want edits quiet, file still tracked | Intentional local divergence | git update-index --skip-worktree |
git rm --cached <path>, then commit — from that commit forward, .gitignore owns the path.- Rule printed? The problem lives in the .gitignore file: rule order, negation traps, or which ignore file owns the path.
- Silence? Confirm with
git ls-files --error-unmatch <path>, thengit rm --cached <path>and commit the removal. - Negation trap:
build/ignores the directory wholesale, so!build/keep.meis a dead letter. Usebuild/*then!build/keep.me; the last matching rule wins.
If it was a secret, untracking is not the fix. The file stays in history — removing it from the latest commit is not enough. Rotating the credential is the real fix; history rewriting is the cosmetic one.
the three scopes, and the scriptable exit codes
git check-ignore -q debug.log && echo ignored || echo tracked-or-unruly # exit codes: 0 = ignored, 1 = not ignored git rm --cached .env git commit -m "stop tracking .env" git check-ignore -v .env # bulk: git rm -r --cached . → git add .
Rules live in three places: .gitignore in the repo (committed, shared), .git/info/exclude (per-clone, never committed), and your global ignore file (core.excludesFile, per-machine). check-ignore -v names the source directly: file, then line-number:pattern, then path. Verify: git check-ignore -v debug.log → .gitignore:3:*.log debug.log.
- tested on: git 2.4x · Linux · macOS · Windows · VS Code SCM view
- diagnosis: git check-ignore -v · git ls-files --error-unmatch
- index immunity · last-match-wins · directory negation · exit codes 0/1 — git's documented behaviour
- first published 2026-09-17