Real Clutter, Surveyed
A macOS system file, a generated build directory, and a secrets file — all genuinely untracked and cluttering every status check.
One Real File, Four Real Rules
printf '.DS_Store\nbuild/\n.env\nnotes.txt\n' > .gitignore
git status
All four are now genuinely invisible to git status — only .gitignore itself shows up as new.
Debugging Exactly Which Rule Matches
Shows the exact file and line number of the rule responsible — genuinely useful the moment a pattern does not behave as expected.
The Real Gotcha: Already-Tracked Files Are Immune
# A secret committed BEFORE being added to .gitignore stays tracked:
git add .gitignore # adding the pattern later does NOT untrack it
git rm --cached db-credentials.txt # this genuinely does
.gitignore only ever governs files git does not yet know about. Untracking an already-committed file requires git rm --cached — it removes the file from tracking while genuinely leaving it on disk.
Real projects accumulate real noise: a .DS_Store macOS leaves behind, a build/ directory full of generated output, a .env file holding a real API key. None of these belong in history — .gitignore is the real, permanent contract that keeps git from ever offering to track them.
But .gitignore has one genuinely important limit: it only affects files git does not already know about. A secret committed BEFORE it was added to .gitignore stays tracked regardless — this lesson hits that exact gotcha for real, and fixes it the right way, with git rm --cached.
.gitignore
A real, plain-text file listing patterns for content git should never offer to track. Confirmed directly in this lab: matching files disappear entirely from git status, as if they were not there at all.
git check-ignore -v <file>
Reports exactly which .gitignore line is causing a specific file to be ignored, including the file and line number. Confirmed directly in this lab pinpointing exactly ".gitignore:3:.env" for the .env rule.
git rm --cached <file>
Removes a file from git's tracking (staging it for a "deleted" commit) while genuinely leaving the actual file untouched on disk. Confirmed directly in this lab: db-credentials.txt was gone from git status afterward but still fully present via ls.
Takeaways
- .gitignore genuinely hides matching files from git status and git add — but only files git does not already track
- git check-ignore -v <file> pinpoints the exact rule (file and line number) responsible for ignoring something
- Adding an already-tracked file to .gitignore changes nothing on its own — git rm --cached is the real fix
- git rm --cached removes a file from tracking while genuinely leaving it untouched on disk
- A real secret committed by mistake should be rotated, not just untracked — history still remembers the old value