mrsn0.3.0
auth
live

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.

2026-11-232 min read

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)
SymptomReal causeFix
Ignored file still shows in git statusCommitted before the rule — in the indexgit rm --cached + commit
git add . keeps re-adding itSame: tracking beats ignoringBulk: git rm -r --cached . → git add .
!build/keep.me never rescues the fileParent dir excluded — git skips it wholesalebuild/* + !build/keep.me
A later rule overrides yoursLast matching rule winsReorder, or read the winner via check-ignore -v
Rule works for teammates, not you (or reverse)Nested .gitignore / exclude / global mismatchMatch the scope table below
VS Code shows it modified after ignoringTracked — or a stale SCM viewgit rm --cached; reload window first
Want edits quiet, file still trackedIntentional local divergencegit update-index --skip-worktree
The law: tracking beats ignoring. git rm --cached <path>, then commit — from that commit forward, .gitignore owns the path.
  1. Rule printed? The problem lives in the .gitignore file: rule order, negation traps, or which ignore file owns the path.
  2. Silence? Confirm with git ls-files --error-unmatch <path>, then git rm --cached <path> and commit the removal.
  3. Negation trap: build/ ignores the directory wholesale, so !build/keep.me is a dead letter. Use build/* 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
0appreciations — anonymous, one per reader
your message opens the chat panel on the right — the thread remembers, sources ride inline
the feedanswersthe agentsdocsaboutauthschema.jsonopenapi.jsonllms.txt