Lesson Completion
Back to course

.gitignore

Beginner
10 minutes4.8GitPlay to LearnLearn by Story

The Hook (The "Byte-Sized" Intro)

In 2015, a developer accidentally pushed their AWS secret keys to a public GitHub repo. Within minutes, bots picked them up and spun up $14,000 worth of EC2 instances for crypto mining. A .gitignore file with one line — .env — would have prevented it. Your repo is not a junk drawer, and .gitignore is the lock on the drawer that keeps secrets, build artifacts, and OS junk out.

📖 What is .gitignore?

.gitignore is a text file that tells Git which files and directories to ignore. Ignored files won't appear in git status, won't be staged by git add ., and won't be committed. It's your first line of defense against repo clutter and accidental secret exposure.

Conceptual Clarity

  • .gitignore uses glob patterns to match files and directories
  • It applies to untracked files only — files already tracked are NOT affected (use git rm --cached to untrack first)
  • You can have multiple .gitignore files in subdirectories — each applies to its own directory and below
  • A global gitignore (~/.gitignore_global) ignores patterns across ALL your repos

Pattern syntax:

PatternMatches
*.logAll files ending in .log
build/The entire build directory
!important.logException — track this even though *.log says ignore
**/temptemp directory anywhere in the tree
doc/*.txtdoc/notes.txt but NOT doc/deep/notes.txt
doc/**/*.txtAny .txt file under doc/ at any depth

Real-Life Analogy

.gitignore is like telling your backup service: "Don't back up the Trash folder, temp files, or anything in Downloads." It keeps your backups (commits) focused on what actually matters.

Visual Architecture

flowchart TD ALL["📂 All Files"] --> FILTER{".gitignore<br/>rules"} FILTER -->|"Matches"| IGN["🚫 Ignored<br/>(invisible to Git)"] FILTER -->|"No match"| TRACK["✅ Tracked<br/>(visible to Git)"] style ALL fill:#1a1a2e,stroke:#e94560,color:#e94560 style FILTER fill:#0f3460,stroke:#ffd700,color:#ffd700 style IGN fill:#2d1b1b,stroke:#e94560,color:#e94560 style TRACK fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb

Why It Matters

  • Security: Prevents .env files, API keys, and credentials from being committed.
  • Repo size: Keeps large generated files (node_modules/, build/, dist/) out.
  • Clean diffs: OS-specific files (.DS_Store, Thumbs.db) won't pollute your commits.
  • Collaboration: Team members with different editors won't commit their IDE config files.

Code

bash
# ─── Create a .gitignore file ─── cat > .gitignore << 'EOF' # Dependencies node_modules/ vendor/ # Build output dist/ build/ *.min.js *.min.css # Environment & secrets .env .env.local *.pem *.key # OS files .DS_Store Thumbs.db # Editor/IDE files .vscode/ .idea/ *.swp *.swo # Logs *.log logs/ # Coverage reports coverage/ .nyc_output/ EOF # ─── Verify: these files won't appear in status ─── git status # .DS_Store, node_modules/, .env — all hidden # ─── Check if a specific file is ignored ─── git check-ignore -v .DS_Store # Output: .gitignore:17:.DS_Store .DS_Store # ─── Check if a file would be ignored ─── git check-ignore -v src/app.js # No output = not ignored (tracked normally) # ─── Force-add an ignored file (override) ─── git add -f important-build-file.js # Use sparingly — better to fix .gitignore patterns # ─── Set up a global gitignore ─── git config --global core.excludesfile ~/.gitignore_global echo ".DS_Store" >> ~/.gitignore_global echo ".idea/" >> ~/.gitignore_global

Starter Templates by Project Type

Project TypeKey Ignore Entries
Node.jsnode_modules/, .env, dist/, *.log
Python__pycache__/, *.pyc, .venv/, .env
Javatarget/, *.class, .idea/, *.jar
React/Next.jsnode_modules/, .next/, out/, .env.local
General.DS_Store, Thumbs.db, .vscode/, *.swp

Pro tip: Visit github.com/github/gitignore for comprehensive templates.

Key Takeaways

  • .gitignore prevents files from being tracked — secrets, build output, OS junk, IDE config.
  • It only affects untracked files — already tracked files need git rm --cached first.
  • Use git check-ignore -v <file> to debug why a file is (or isn't) ignored.
  • Set up a global gitignore for OS/editor files that apply to ALL repos.

Interview Prep

  • Q: Does .gitignore remove files that are already tracked? A: No. .gitignore only prevents untracked files from being staged. To stop tracking a file that's already committed, you must first run git rm --cached <file>, then add the pattern to .gitignore, and commit.

  • Q: How do you debug why a file is or isn't being ignored? A: Use git check-ignore -v <file>. It shows which .gitignore file and which pattern matched (or no output if the file isn't ignored).

  • Q: What is a global gitignore and when should you use it? A: A global gitignore (configured via core.excludesfile) applies ignore patterns across all repositories on your machine. Use it for OS-specific files (.DS_Store, Thumbs.db) and editor configs (.vscode/, .idea/) that are personal to your setup, not project-specific.

Topics Covered

Git BasicsGit Fundamentals

Tags

#git#gitignore#ignore#beginner-friendly

Last Updated

2026-02-12