.gitignore

๐Ÿ“˜ Git & GitHub ๐Ÿ“— Chapter 29 of 40 ๐Ÿ“‚ Phase 9: Git Best Practices ๐Ÿ—“๏ธ 2026 Edition
๐Ÿ“Œ Covered in this chapter: Ignoring Folders & Files ยท Environment Files ยท Global gitignore ยท Already-Tracked Files

Prevent unwanted files โ€” dependencies, build output, secrets โ€” from ever being tracked by Git using a .gitignore file.

1.gitignore โ€” What You'll Learn

Prevent unwanted files โ€” dependencies, build output, secrets โ€” from ever being tracked by Git using a .gitignore file.

Here's everything this chapter covers, in the order you'll learn it:

  • Why .gitignore is essential
  • Ignoring entire folders (like node_modules)
  • Ignoring specific files
  • Ignoring environment files (.env)
  • Ignoring build output (dist, build)
  • Ignoring dependency folders
  • Setting up a global gitignore for your machine
  • Handling files that are already tracked (needs git rm --cached)
  • Negation patterns (un-ignoring specific files)
  • Using language-specific .gitignore templates
2Working Example
๐Ÿ’ป Example: .gitignore
Text
node_modules/
.env
dist/
build/
*.log
.vscode/
3Best Practices & Common Pitfalls
๐Ÿ’ก Key things to remember:
  • If a file is already tracked by Git, adding it to .gitignore afterward won't stop tracking it โ€” you must first run git rm --cached to untrack it.
โ“ Frequently Asked Questions (FAQ)

Q What's the most important thing to understand about .gitignore?

Focus on: Ignoring Folders & Files ยท Environment Files ยท Global gitignore ยท Already-Tracked Files. These are the core building blocks this chapter's examples are built around, and they show up repeatedly in later chapters of this course.

Q Is .gitignore something I'll use often in real projects?

Yes โ€” every concept in this chapter reflects a real, everyday part of professional Git and GitHub workflows, not just a theoretical exercise.

OC
Written by Our Compiler Technical Editorial Team
Reviewed for accuracy ยท Last updated August 2026