Chapter 43: Git Ignore and .gitignore
Git Ignore and the .gitignore file
This is one of those things that feels small and boring at first… until the day you accidentally commit your .env file with database passwords, or node_modules/ with 500 MB of dependencies, or your IDE settings, or temporary build files — and then you realize:
.gitignore is your safety net against accidentally publishing private, useless, or huge files.
Almost every real-world Git project has a .gitignore file from day 1. Teams get very angry when someone commits secrets or junk. Interviewers often ask: “Show me your .gitignore” or “Why do we ignore node_modules?”
So today I’m going to explain everything you need to know about Git ignore — like I’m sitting next to you creating one together — very slowly, with real examples, patterns you will copy-paste in 2026, and the exact mental model so you never forget.
1. What does Git ignore actually do?
.gitignore tells Git: “Please pretend these files and folders do not exist — never show them in git status, never stage them, never commit them, never push them.”
Important points:
- It only affects untracked files (files Git has never seen before)
- If a file is already tracked (you committed it once), .gitignore does not magically remove it — you have to untrack it manually
- .gitignore itself is tracked and committed (so the whole team gets the same ignore rules)
Think of it like:
- Your project folder = a messy room
- Git = a photographer who takes snapshots of the room
- .gitignore = a list you give the photographer saying “don’t bother photographing the trash bin, the laundry pile, the secret diary, the huge pile of take-away boxes”
2. Where does the .gitignore file live?
Three main places (Git reads them in this order — later ones override earlier):
| Level | Location | Affects which repositories? | Who usually creates it? |
|---|---|---|---|
| Global | ~/.gitignore_global or ~/.config/git/ignore | All repositories on your computer | You (personal ignores like .DS_Store) |
| Per-repo | .gitignore in project root | Only this repository | The project team (most important one) |
| Per-directory | .gitignore in any sub-folder | Only that folder and below | Rare — usually only root .gitignore |
Most projects only use the root .gitignore — that’s enough 95% of the time.
3. Real Example – Create & Use .gitignore Right Now
Let’s make a tiny project and see how it works.
|
0 1 2 3 4 5 6 7 8 |
mkdir gitignore-demo cd gitignore-demo git init |
Step 1 – Create some files Git should ignore
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Secret file echo "DB_PASSWORD=supersecret123" > .env # Node.js project junk mkdir node_modules touch node_modules/some-package.js # IDE files touch .vscode/settings.json # Temporary files touch temp.log cache.tmp # Important file we DO want to track echo "# My Todo App" > README.md |
Now:
|
0 1 2 3 4 5 6 |
git status |
You see everything in red — Git wants to track them all — bad!
Step 2 – Create .gitignore
Create file .gitignore in the root:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# Common ignores for Node.js / JavaScript projects (2026 standard) node_modules/ npm-debug.log yarn-error.log # Environment variables / secrets .env .env.local .env.development .env.test .env.production # IDE / editor files .vscode/ .idea/ *.sublime-project *.sublime-workspace # OS junk .DS_Store Thumbs.db # Temporary / log files *.log *.tmp cache/ temp/ |
Save it.
Step 3 – See the magic
|
0 1 2 3 4 5 6 |
git status |
Now you see:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
On branch main No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) .gitignore README.md |
→ .env, node_modules/, .vscode/, logs — all disappeared from status! → Git pretends they don’t exist anymore
Step 4 – Commit the .gitignore itself
|
0 1 2 3 4 5 6 7 |
git add .gitignore README.md git commit -m "chore: add .gitignore and initial README" |
Now the whole team (and future you) will inherit these ignore rules.
4. Most Important .gitignore Patterns (copy-paste these in 2026)
| Pattern | What it ignores | Example use case |
|---|---|---|
| node_modules/ | Entire folder | Every JavaScript/TypeScript project |
| .env* | All .env files + variants | Secrets, API keys, database URLs |
| *.log | All log files | Application / debug logs |
| dist/ or build/ | Build output folders | React/Vite/Angular/Webpack builds |
| .DS_Store | macOS junk | Every macOS user |
| *.pyc / __pycache__/ | Python compiled files | Python projects |
| coverage/ | Test coverage reports | Jest / Mocha / pytest |
| *.swp / *.swo | Vim swap files | Vim users |
| Thumbs.db | Windows thumbnail cache | Windows users |
| *.bak / *.tmp | Backup / temporary files | General cleanup |
5. Advanced but Useful .gitignore Tricks
Negation (!) — ignore folder but keep one file
logs/
!logs/important.log ← keep this oneIgnore everything except certain files
*
!README.md
!.gitignoreGlobal ignore (your personal ignores)
|
0 1 2 3 4 5 6 |
git config --global core.excludesfile ~/.gitignore_global |
Then in ~/.gitignore_global:
|
0 1 2 3 4 5 6 7 8 |
.DS_Store .vscode/ .idea/ |
6. Common Gotchas & Fixes (2026 reality)
| Problem | Reason & Fix |
|---|---|
| File still shows in git status after .gitignore | File was already committed → git rm –cached filename then commit |
| .gitignore not working in subfolder | Rules are relative — put .gitignore in that subfolder if needed |
| Large files already pushed | .gitignore doesn’t remove history — use git filter-repo or BFG Repo-Cleaner |
| Teammate doesn’t have .gitignore | Commit it! It’s a normal file — everyone gets it on git pull |
Got the .gitignore feeling now?
.gitignore = your project’s “do not photograph” list — prevents Git from ever tracking junk, secrets, or huge folders
It’s one of the first files every serious project creates — and one of the most embarrassing things to forget.
Next?
- Want to create a perfect .gitignore for Node.js / React / Python / Java project?
- See how to remove already-committed secrets safely?
- Or move to one final big summary of all Git concepts we covered?
Just tell me — we’ll finish strong. You’ve now covered Git from zero to advanced — incredible work! 🚀
