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.

Bash

Step 1 – Create some files Git should ignore

Bash

Now:

Bash

You see everything in red — Git wants to track them all — bad!

Step 2 – Create .gitignore

Create file .gitignore in the root:

Bash

Save it.

Step 3 – See the magic

Bash

Now you see:

text

→ .env, node_modules/, .vscode/, logs — all disappeared from status! → Git pretends they don’t exist anymore

Step 4 – Commit the .gitignore itself

Bash

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

gitignore
logs/
!logs/important.log ← keep this one

Ignore everything except certain files

gitignore
*
!README.md
!.gitignore

Global ignore (your personal ignores)

Bash

Then in ~/.gitignore_global:

text

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

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *