What Is .gitignore?
Not every file in your project folder belongs in version control. Some files contain secrets, others are generated automatically, and some are just clutter. The .gitignore file tells Git which files and folders to completely ignore — it won't track them, won't show them in status, and won't let you accidentally commit them.
Why Ignoring Files Matters
Several types of files should never be committed:
Secrets and credentials like API keys, passwords, and environment variables in .env files. Committing these exposes them to anyone who can see your repository.
Dependencies like node_modules/ or Python virtual environments. These can be thousands of files that anyone can regenerate from your package list.
Generated files like compiled code, build outputs, and cache folders. These clutter your history with changes that don't matter.
System files like .DS_Store on macOS or Thumbs.db on Windows. These are operating system artifacts, not part of your project.
Creating a .gitignore File
Create a file named .gitignore in your project's root folder. Each line specifies a pattern to ignore:
# Dependencies
node_modules/
__pycache__/
venv/
# Environment and secrets
.env
.env.local
*.key
# Generated files
dist/
build/
*.log
# System files
.DS_Store
Thumbs.db
Lines starting with # are comments. The / at the end indicates a folder. The * is a wildcard matching any characters.
Common Patterns
Here are patterns you'll use frequently:
*.log— ignore all files ending in.lognode_modules/— ignore the entire folder.env— ignore this specific filebuild/— ignore the build output folder*.pyc— ignore compiled Python files
Add .gitignore Early
Create your .gitignore file at the start of your project, before your first commit. If you've already committed files you want to ignore, Git will continue tracking them even after you add them to .gitignore. You'd need to explicitly remove them from tracking.
Finding Templates
You don't need to memorize what to ignore. GitHub maintains templates for different languages and frameworks at github.com/github/gitignore. When you create a new repository on GitHub, it can add an appropriate .gitignore automatically.
You can also ask an AI: "What should I put in .gitignore for a Python Flask project?" and get a solid starting point.