The Hook (The "Byte-Sized" Intro)
Your repo should contain source code, not node_modules/, .env secrets, or .DS_Store. .gitignore tells Git what to pretend doesn't exist. Without it, your commits bloat with build artifacts, your secrets leak, and every teammate's OS files pollute the diff.
📖 What is Ignoring Files?
.gitignore is a file that tells Git which files and directories to exclude from tracking. Ignored files won't appear in git status or be included in commits.
Conceptual Clarity
Ignore levels:
| Level | File | Scope |
|---|---|---|
| Repository-wide | .gitignore (committed) | Shared with team |
| Personal | .git/info/exclude | Only your machine |
| Global | ~/.gitignore_global | All your repos |
Pattern syntax:
| Pattern | Meaning |
|---|---|
file.txt | Ignore file.txt in any directory |
/file.txt | Ignore file.txt in root only |
*.log | Ignore all .log files |
dir/ | Ignore directory dir |
!important.log | Don't ignore this file (exception) |
**/logs | Ignore logs folder at any depth |
Common ignores by ecosystem:
| Ecosystem | Ignored |
|---|---|
| Node.js | node_modules/, .env |
| Python | __pycache__/, *.pyc, .venv/ |
| Java | target/, *.class, .idea/ |
| General | .DS_Store, *.log, .env |
Real-Life Analogy
.gitignore is a "Do Not Pack" list for your suitcase. You want clothes (source code) but not the laundry basket (build output), the safe (secrets), or the furniture (OS files).
Visual Architecture
Why It Matters
- Security:
.envfiles with secrets must never be committed. - Repo size:
node_modulescan be 500MB+ — keep it out. - Clean diffs: OS files like
.DS_Storecreate noise. - Team hygiene: Everyone sees the same clean repo.
Code
# ─── Create a .gitignore ───
cat > .gitignore << 'EOF'
# Dependencies
node_modules/
.venv/
# Build output
dist/
build/
# Secrets
.env
.env.local
# OS files
.DS_Store
Thumbs.db
# Logs
*.log
EOF
# ─── Stop tracking an already-tracked file ───
git rm --cached .env
# Removes from Git tracking but keeps the file on disk
# ─── See what's being ignored ───
git status --ignored
# ─── Check if a file is ignored ───
git check-ignore -v file.txt
# Shows which .gitignore rule matches
# ─── Global ignore ───
git config --global core.excludesfile ~/.gitignore_global
echo ".DS_Store" >> ~/.gitignore_globalKey Takeaways
.gitignorekeeps build artifacts, secrets, and OS files out of the repo.- Already-tracked files must be removed with
git rm --cachedto start ignoring. - Use
git check-ignore -vto debug which rule is matching. - Set a global ignore for OS files (
.DS_Store,Thumbs.db).
Interview Prep
-
Q: How do you stop tracking a file that's already committed? A:
git rm --cached <file>removes it from Git's tracking without deleting the file from disk. Then add the file to.gitignoreand commit both changes. -
Q: What is the difference between
.gitignoreand.git/info/exclude? A:.gitignoreis committed and shared with the team..git/info/excludeis local to your machine and not committed — use it for personal ignores that shouldn't affect teammates. -
Q: How do you debug why a file is or isn't being ignored? A:
git check-ignore -v <file>shows which.gitignorefile and which rule matches the file. If no output, the file isn't being ignored.