Ignoring Files with .gitignore
What you'll learn: How to tell Git to intentionally ignore certain files so they never get tracked or committed.
Why Ignore Files?
Not every file in your project folder belongs in version control. Some files are temporary, machine-specific, or contain sensitive information. Examples include:
- Build artifacts (compiled code)
- Dependency folders (
node_modules/,venv/) - Log files
- API keys or passwords
- Operating system files (
.DS_Storeon Mac,Thumbs.dbon Windows)
Committing these clutters your repository, wastes space, and can expose secrets.
What is .gitignore?
The .gitignore file is a special text file you place in your repository's root directory. It lists patterns for files and folders Git should ignore—meaning Git won't track them, won't show them in git status, and won't let you accidentally commit them.
Think of .gitignore as a "do not disturb" list for Git.
Creating and Using .gitignore
Create a file named exactly .gitignore (note the dot at the beginning) in your project folder. Inside, list patterns—one per line:
# Ignore all log files
*.log
# Ignore the entire build directory
build/
# Ignore environment variables file
.env
The * wildcard matches anything. The # symbol starts a comment.
Once saved, Git immediately respects these rules. Files matching these patterns won't appear in git status as untracked.
Common Patterns
*.log– Ignore all files ending in.logtemp/– Ignore the entiretempdirectoryconfig.local– Ignore a specific file
Key Takeaway: The .gitignore file lets you exclude files from version control by listing patterns, keeping your repository clean and preventing accidental commits of unwanted files.