Lesson Completion
Back to course

Ignoring Files

Beginner
8 minutes4.7Git

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:

LevelFileScope
Repository-wide.gitignore (committed)Shared with team
Personal.git/info/excludeOnly your machine
Global~/.gitignore_globalAll your repos

Pattern syntax:

PatternMeaning
file.txtIgnore file.txt in any directory
/file.txtIgnore file.txt in root only
*.logIgnore all .log files
dir/Ignore directory dir
!important.logDon't ignore this file (exception)
**/logsIgnore logs folder at any depth

Common ignores by ecosystem:

EcosystemIgnored
Node.jsnode_modules/, .env
Python__pycache__/, *.pyc, .venv/
Javatarget/, *.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

flowchart TD ALL["All Files"] --> GITIGNORE[".gitignore Filter"] GITIGNORE -->|"Tracked"| REPO["📦 Repository"] GITIGNORE -->|"Ignored"| SKIP["🚫 Excluded<br/>node_modules, .env"] style ALL fill:#1a1a2e,stroke:#53d8fb,color:#53d8fb style SKIP fill:#2d1b1b,stroke:#e94560,color:#e94560 style REPO fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb

Why It Matters

  • Security: .env files with secrets must never be committed.
  • Repo size: node_modules can be 500MB+ — keep it out.
  • Clean diffs: OS files like .DS_Store create noise.
  • Team hygiene: Everyone sees the same clean repo.

Code

bash
# ─── 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_global

Key Takeaways

  • .gitignore keeps build artifacts, secrets, and OS files out of the repo.
  • Already-tracked files must be removed with git rm --cached to start ignoring.
  • Use git check-ignore -v to 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 .gitignore and commit both changes.

  • Q: What is the difference between .gitignore and .git/info/exclude? A: .gitignore is committed and shared with the team. .git/info/exclude is 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 .gitignore file and which rule matches the file. If no output, the file isn't being ignored.

Topics Covered

Git ConfigurationGit Basics

Tags

#git#gitignore#config#beginner

Last Updated

2026-02-13