Ignoring Files
Ignoring Files
Not every file in your project should be tracked by Git. Build artifacts, dependencies, temporary files, and secrets have no place in version control. Git’s ignore mechanism keeps them out automatically.
The .gitignore File
Create a file named .gitignore in your repository root. List patterns for
files Git should ignore, one per line:
Dependencies
node_modules/ vendor/ *.pyc pycache/
Build outputs
dist/ build/ _.exe _.o
IDE files
.vscode/ .idea/ *.swp
Environment files
.env .env.local
OS files
.DS_Store Thumbs.db
How patterns work:
file.txt- ignores a specific file*.log- ignores all files ending in .logtemp/- ignores entire directory!important.log- exception: don’t ignore this file even if it matches another pattern
Why Ignore Files?
Dependencies (node_modules, vendor): These can be recreated from
package.json or similar. Including them bloats your repository and creates
merge conflicts when dependencies update.
Build outputs (dist, .exe, .jar): Generated from source code. Tracking them wastes space and creates confusion—which is the source of truth, the code or the binary?
Environment files (.env): Contain secrets like API keys and database passwords. Once committed, they’re in Git history forever—even if you delete them later. Never commit secrets.
IDE files (.vscode, .idea): Personal preferences. Your teammate might use a different editor. Don’t force your settings on others.
OS files (.DS_Store, Thumbs.db): System-generated metadata. Irrelevant to the actual project.
When to Create .gitignore
Create .gitignore before your first commit. It’s much easier to ignore files
from the start than to remove them after they’re already tracked.
Template Collections
Don’t build .gitignore from scratch. GitHub maintains templates for every
language and framework:
Starting a Python project?
curl https://raw.githubusercontent.com/github/gitignore/main/Python.gitignore > .gitignoreNode.js?
curl https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore > .gitignoreCustomize the template for your specific project needs.
Already Committed Files
Ignoring a file doesn’t remove it from Git if it’s already tracked. You must explicitly untrack it:
# Remove from Git but keep on disk
git rm --cached filename
# Remove entire directory from Git
git rm -r --cached directory/
# Commit the removal
git commit -m "Stop tracking build artifacts"The --cached flag means “remove from Git’s tracking but don’t delete the
actual file.” Without it, Git deletes the file entirely.
Global Gitignore
Some files you want ignored in every repository—OS files, editor preferences, personal notes. Create a global ignore file:
# Create global ignore file
touch ~/.gitignore_global
# Configure Git to use it
git config --global core.excludesfile ~/.gitignore_globalAdd your universal ignores:
# ~/.gitignore_global
.DS_Store
Thumbs.db
*.swp
.vscode/
notes.txt
TODO.mdWhy both global and local?
- Global: Personal preferences (editor, OS)
- Local (repo-specific): Project requirements (dependencies, build outputs)
Checking What’s Ignored
See if a file is ignored:
git check-ignore -v filenameThis shows which .gitignore rule is matching the file.
List all ignored files:
git status --ignoredCommon Mistakes
Committing .env files: Once committed, secrets are in history. You must
rewrite history (complicated) or rotate the secrets (better). Prevention is
easier—add .env to .gitignore immediately.
Ignoring too much: Don’t ignore source code or configuration files your project needs. Teammates should be able to clone and run the project without hunting for missing files.
Inconsistent ignores: Team members using different .gitignore files create
confusion. The repository’s .gitignore is the source of truth—everyone should
use the same one.
Ignoring Without .gitignore
Sometimes you want to ignore files temporarily without modifying .gitignore:
# Edit .git/info/exclude (same syntax as .gitignore)
# These ignores are local—not shared with your teamUse this for experimental files or personal scripts you don’t want tracked but
also don’t want to formalize in .gitignore.
Debugging Ignore Problems
File still showing as untracked:
- Check for typos in
.gitignore - Ensure
.gitignoreis in the repository root (or the appropriate subdirectory) - Verify the file wasn’t already tracked before adding the ignore rule
Ignore rule not working:
- Git tracks files, not directories. To ignore an empty directory, you can’t—Git doesn’t track empty directories.
- Wildcards might not match the way you expect. Test with
git check-ignore -v
The Commit .gitignore Pattern
After creating or updating .gitignore, commit it:
git add .gitignore
git commit -m "Add gitignore rules for Python project"This shares your ignore rules with your team. Everyone benefits from a clean, consistent repository.
What’s Next?
With ignored files handled, you’re ready to learn the core workflow: making changes, staging them, and committing snapshots of your work. This is where Git’s power starts to become real.