Module lessons (4/5)
.gitignore rules & untracking leaked files
Keeping Noise and Secrets Out of Git
Build folders (dist/), runtime logs (*.log), and private credentials (.env) belong only on the local machine or secret vault — never inside .git/objects.
Writing paths into .gitignore instructs git status and git add to skip matching untracked paths automatically:
.gitignore Pattern | Matches | Does NOT Match | Typical Purpose |
|---|---|---|---|
.env | Exact file .env at any depth | .env.example template | API keys & DB passwords |
*.log | debug.log, logs/error.log | logger.ts | Runtime log output |
dist/ | Whole directory dist/ | Regular file dist | Bundled build artifacts |
!dist/keep.txt | Re-includes matching ignored path | Parent dir itself if ignored | Keeping template/placeholder |
Test .gitignore Filtering Visually
Toggle *.log inside the .gitignore rules switch of the interactive simulator and watch how build.log immediately jumps between Ignored and Untracked:
Example 1 — Writing Multi-Line Ignore Rules Cleanly
Create .gitignore and commit it so every teammate shares identical ignore rules right away.
echo ".env" > .gitignore
echo "*.log" >> .gitignore
echo "dist/" >> .gitignore
git status # .env, logs/debug.log, and dist/ disappear from untracked!Example 2 — Why Already-Tracked Files Ignore .gitignore
Adding a rule to .gitignore does not untrack a file that was already committed earlier — Git keeps tracking anything present inside the Staging Index!
# Oops: secrets.env was already committed earlier!
echo "secrets.env" >> .gitignore
git status # Still tracks edits to secrets.env!Example 3 — Untracking Without Deleting on Disk (git rm --cached)
Passing --cached removes the file strictly from the Staging Index (so the next commit drops it) while leaving your precious .env file untouched on disk.
git rm --cached .env # Staged deletion from Index ONLY (disk copy kept!)
echo ".env" >> .gitignore
git add .gitignore
git commit -m "security: untrack .env while preserving local file"Example 4 — Force-Adding vs Exception Rules
Need to commit one sample template file even when an ignore rule catches similar names? Prefer a negation rule (!.env.example) rather than git add -f.
.env*
!.env.example
logs/
dist/
Step 1 — Write .gitignore rules to silence secrets and build artifacts
Create a .gitignore file that ignores '.env', '*.log', and 'dist/', then run git status so only .gitignore and src/routes.ts appear untracked.
Show hint
Append .env, *.log, and dist/ into .gitignore, then run git status.
Solution available after 3 attempts
Step 2 — Shield the workspace and commit only source files
Write .gitignore rules covering .env, *.log, and dist/, stage .gitignore alongside src/routes.ts, and commit cleanly without tracking .env, logs/debug.log, or dist/bundle.js.
Show hint
Write .gitignore rules first, then git add .gitignore src/routes.ts and git commit -m "...".
Solution available after 3 attempts
Step 3 — Rescue an accidentally committed .env file
Simulate accidentally committing .env, then untrack .env strictly from Git with git rm --cached .env (keeping disk file!), add .env to .gitignore, and commit the security fix.
Show hint
Run git rm --cached .env, write .env into .gitignore, stage .gitignore, and commit.
Solution available after 3 attempts
A teammate adds config.local.json to .gitignore, commits .gitignore, yet Git STILL keeps staging changes to config.local.json. Why?
What is the exact difference between running git rm .env and git rm --cached .env?
git rm --cached .env # vs git rm .env