Skip to main content
Module 1 · Lesson 4 of 54/34 in the course~16 min
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 PatternMatchesDoes NOT MatchTypical Purpose
.envExact file .env at any depth.env.example templateAPI keys & DB passwords
*.logdebug.log, logs/error.loglogger.tsRuntime log output
dist/Whole directory dist/Regular file distBundled build artifacts
!dist/keep.txtRe-includes matching ignored pathParent dir itself if ignoredKeeping 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.

Bash
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!

Bash
# 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.

Bash
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.

Git
.env*
!.env.example
logs/
dist/
Practice Stage

Step 1 — Write .gitignore rules to silence secrets and build artifacts

Exercise#git.m1.l4.e1
Attempts: 0Loading…

Create a .gitignore file that ignores '.env', '*.log', and 'dist/', then run git status so only .gitignore and src/routes.ts appear untracked.

Loading editor…
Show hint

Append .env, *.log, and dist/ into .gitignore, then run git status.

Solution available after 3 attempts

Practice Stage

Step 2 — Shield the workspace and commit only source files

Exercise#git.m1.l4.e2
Attempts: 0Loading…

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.

Loading editor…
Show hint

Write .gitignore rules first, then git add .gitignore src/routes.ts and git commit -m "...".

Solution available after 3 attempts

Practice Stage

Step 3 — Rescue an accidentally committed .env file

Exercise#git.m1.l4.e3
Attempts: 0Loading…

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.

Loading editor…
Show hint

Run git rm --cached .env, write .env into .gitignore, stage .gitignore, and commit.

Solution available after 3 attempts

Git Quiz#git.m1.l4.e4
Ready

A teammate adds config.local.json to .gitignore, commits .gitignore, yet Git STILL keeps staging changes to config.local.json. Why?

Answer options
Git Quiz#git.m1.l4.e5
Ready

What is the exact difference between running git rm .env and git rm --cached .env?

Bash
git rm --cached .env   # vs   git rm .env
Answer options