The first git status in a new project often lists hundreds of files you never meant to share: node_modules, build output, editor settings, a .DS_Store in every folder and, worst of all, the .env file with your database password. A .gitignore file tells Git which untracked files to leave alone, and a good one saves you from noisy commits and accidental leaks.
This guide explains how .gitignore patterns are matched, why some rules seem to be ignored, how to untrack files that were committed before the rule existed, and how to assemble a sensible file for your languages, frameworks and tools in a minute.
How Git reads a .gitignore file
A .gitignore is a plain text file, usually in the repository root, with one pattern per line. Git checks every untracked path against those patterns. Key rules:
- Lines starting with
#are comments, and blank lines are ignored. - Later rules override earlier ones. The last matching pattern wins.
- A
.gitignoreonly affects untracked files. If a file is already committed, adding it to.gitignoredoes nothing until you untrack it. - You can place additional
.gitignorefiles in subfolders; their patterns are relative to that folder.
Pattern syntax at a glance
| Pattern | Matches |
|---|---|
*.log |
Any file ending in .log, in any folder |
build/ |
Any folder named build, anywhere, with everything in it |
/build |
Only build in the same folder as the .gitignore |
docs/*.pdf |
PDFs directly inside docs, not in its subfolders |
docs/**/*.pdf |
PDFs anywhere below docs |
temp?.txt |
temp1.txt, tempA.txt (one character) |
!keep.log |
Re-includes a file that an earlier rule ignored |
Two details catch people out. A leading slash anchors the pattern to the .gitignore's own folder, so /build ignores the root build folder but not src/build. And a pattern containing a slash in the middle (like docs/*.pdf) is also treated as relative to that folder.
Why your negation rule does not work
This looks reasonable but fails:
logs/
!logs/keep.log
Git stops looking inside a folder once the folder itself is excluded, so it never sees keep.log to re-include it. Ignore the folder's contents instead of the folder:
logs/*
!logs/keep.log
The same principle explains the common environment-file pattern:
.env
.env.*
!.env.example
Every .env variant is ignored, while the example file with placeholder values stays in the repository so teammates know which variables to set.
Build a .gitignore for your stack
Writing every rule from memory is error-prone. The .gitignore Generator combines ready-made templates in your browser.
- Open .gitignore Generator. Node.js, Visual Studio Code, macOS and Windows are selected to start with.
- Click a Presets button such as Next.js app, Python / Django, Java / Gradle, Flutter or Go service to replace the selection, or Clear it and start from scratch.
- Use the search box ("python, unity, jetbrains…") or browse the groups: Languages, Frameworks, IDEs & editors, Operating systems and Tools. Click a template to toggle it; there are 50 in total.
- Add project-specific lines under Your own rules (optional), for example
exports/or*.local.json. - Keep Remove rules repeated across templates on so a rule such as
.DS_Storeor*.logappears only once. The status line shows the rule count and how many duplicates were removed. - Use Copy or Download in the .gitignore panel and save it as
.gitignorein the root of your repository.
Each template appears under its own ### Name ### heading, so you can see later why a rule exists and delete one you do not want.
Stop tracking files that are already committed
If a file was committed before you ignored it, Git keeps tracking changes. Remove it from the index while leaving it on disk:
# One file
git rm --cached config/local.json
# A whole folder
git rm -r --cached node_modules
git commit -m "Stop tracking local files"
To re-apply a changed .gitignore to the whole repository at once:
git rm -r --cached .
git add .
git commit -m "Apply .gitignore"
Review git status before committing; this approach restages everything and will show every file the new rules drop.
If the file contained a secret, removing it is not enough. It remains in the repository history and on every clone. Treat the password or key as exposed: rotate it with the provider first, then consider rewriting history with a dedicated tool if the repository is shared.
Useful commands for debugging ignore rules
# Which rule ignores this path?
git check-ignore -v logs/app.log
# List ignored files in the working tree
git status --ignored
git check-ignore -v prints the file, line number and pattern responsible, which is the fastest way to untangle a long .gitignore.
Personal and machine-wide ignores
Not every rule belongs in the shared file. Editor and operating-system files are personal choices, and you can ignore them for all your repositories with a global excludes file:
git config --global core.excludesFile ~/.gitignore_global
For rules that apply to one clone only and should never be committed, add them to .git/info/exclude in that repository. Many teams still include common OS and IDE rules in the project file anyway, because it protects contributors who have no global setup.
Tips
- Commit the
.gitignorefirst, beforegit add ., so the unwanted files never enter history. - Ignore outputs, not inputs. Build folders, caches, logs and dependency folders can be recreated; source, configuration templates and lock files (
package-lock.json,poetry.lock) usually should be committed. - Pair it with the other repository basics. A License Generator creates the
LICENSEfile, and if the project runs in containers, the Docker Run to Compose converter helps you move settings into a Compose file that reads secrets from env files your.gitignoreexcludes. - Keep deployment-related ignore files in mind. Docker uses a separate
.dockerignorewith similar syntax to keep files out of image builds;.gitignorerules do not apply there.
Frequently asked questions
Why is my .gitignore not ignoring a file?
The file is probably already tracked. Run git rm --cached <file> and commit; from then on the rule applies. Also check with git check-ignore -v <file> that the pattern matches the path you expect.
Where should the .gitignore file go?
Put the main file in the repository root. You can add more .gitignore files in subfolders for rules that only apply there.
Should I commit the .gitignore file?
Yes. It is part of the project so every contributor ignores the same files. Use a global excludes file for purely personal rules.
How do I ignore everything in a folder except one file?
Ignore the folder's contents with folder/*, then add !folder/file-to-keep. Ignoring the folder itself with folder/ prevents the exception from working.
Should .env files be in .gitignore?
Yes. Environment files often hold passwords and API keys. Ignore them, commit a .env.example with placeholders, and rotate any secret that was ever committed.