.gitignore — Ignoring Files
Not everything in your project folder belongs in Git. Build artifacts, dependency folders, secret files, and OS junk should be kept out of your repository. The .gitignore file tells Git which files and directories to ignore.
How It Works
Create a file named .gitignore in the root of your repository. Each line is a pattern that Git will skip when tracking files:
node_modules/
.env
*.log
.DS_Store
Files matching these patterns will not appear in git status and will not be staged by git add ..
Common Patterns
| Pattern | What it matches |
|---|---|
node_modules/ | The entire node_modules directory |
*.log | Any file ending in .log |
.env | A specific file named .env |
dist/ | A build output directory |
*.tmp | All temporary files |
.DS_Store | macOS Finder metadata |
__pycache__/ | Python cache directory |
*.o | Compiled object files |
Pattern Rules
- Blank lines are ignored (use them for readability)
- Lines starting with
#are comments - A trailing
/means "directory only" - A leading
!negates a pattern (re-includes a file) *matches anything except/**matches across directories
Example with negation:
*.log
!important.log
This ignores all .log files except important.log.
Try it!
The repository is already initialized. Create some files that should be ignored:
- Create files to ignore:
echo "secret" > .envandecho "debug info" > debug.log - Check status:
git status(both files appear as untracked) - Create a
.gitignorefile:echo -e ".env\n*.log" > .gitignore - Check status again:
git status(only.gitignoreappears now!) - Stage and commit:
git add .gitignorethengit commit -m "Add gitignore file" - Verify ignored files are still present on disk:
ls -a
The files are still in your folder — Git just pretends they don't exist.
Important: Already-Tracked Files
If a file is already tracked by Git (previously committed), adding it to .gitignore won't remove it. You need to untrack it first:
git rm --cached secret.txt
Then commit, and from that point on .gitignore will apply.