How to Write a .gitignore That Actually Works
Build a .gitignore for your stack, understand how Git matches patterns, keep secrets out of commits and test which paths are ignored before you push.

How to Write a .gitignore That Actually Works
A .gitignore file tells Git which files to leave out of version control. Get it right and your repository holds only source code and the files needed to build it. Get it wrong and you push hundreds of megabytes of dependencies, leak an API key in a .env file, or annoy every teammate with your editor's settings. The free .gitignore Generator builds a correct file from templates and lets you test it, but it helps to understand what the patterns mean.
What belongs in .gitignore
- Dependencies that a package manager can reinstall: node_modules, vendor, .venv.
- Build output: dist, build, target, bin, obj, .next.
- Logs and caches: *.log, .pytest_cache, .eslintcache.
- Editor and operating system files: .vscode (mostly), .idea, .DS_Store, Thumbs.db.
- Secrets: .env, private keys, certificates.
What does not belong there: anything another developer needs to build or run the project, such as lock files (package-lock.json, poetry.lock, Cargo.lock for applications), configuration templates such as .env.example, and editor settings your team deliberately shares.
How Git reads patterns
Each line is a pattern. A few rules cover almost everything:
- Plain names match anywhere. *.log ignores log files in every folder.
- A leading slash anchors to the repository root. /build ignores only the top-level build folder, not src/build.
- A trailing slash means folders only. node_modules/ will not match a file that happens to be called node_modules.
- Two stars cross folders. docs/**/*.tmp matches docs/a/b/c.tmp.
- An exclamation mark re-includes. After .env.* you can add !.env.example to keep the template.
- The last matching line wins. Put negations after the rule they override.
One subtlety trips people up: if a whole folder is ignored, Git never looks inside it, so you cannot re-include a single file within an ignored folder. Ignore the folder's contents (folder/*) instead of the folder itself if you need exceptions.
Building the file
Open the generator and tick what your project uses: a language such as Node or Python, a framework such as Next.js or Django, build tools, your editor and your operating system. The templates, based on the community github/gitignore collection, are merged into one file with comments marking each section, and duplicate lines are removed. Add project-specific rules, such as an uploads folder or local database files, in the custom box.
Testing before you commit
Type a few paths into the tester, one per line, ending folders with a slash. Each path shows whether it would be ignored and which rule decided. Typical checks:
- node_modules/react/index.js should be ignored.
- .env should be ignored, .env.example tracked.
- src/build/helper.js should stay tracked if you only meant the top-level build folder.
On your own machine, git check-ignore -v path gives the same answer from Git itself.
Files that are already tracked
A .gitignore only affects untracked files. If node_modules or a .env file was committed before, Git keeps tracking it. Remove it from the index once, keeping the file on disk:
git rm -r --cached node_modules git commit -m "Stop tracking node_modules"
If a secret was ever committed, it remains in the history. Removing it from the latest commit does not make it safe: rotate the key or password, and rewrite history only if you understand the consequences for everyone who cloned the repository.
A global ignore file for personal clutter
Editor and operating system files are personal. Instead of adding .DS_Store or .idea to every project, put them in a global file once:
git config --global core.excludesFile ~/.gitignore_global
Then the project .gitignore can focus on what the project itself produces.
A quick checklist
- Dependencies, build output, logs and caches ignored.
- Secrets ignored, templates kept with a negation.
- Lock files committed for applications.
- No folder-wide ignore hiding a file you need.
- Tested with a few real paths.
Everything in the generator runs in your browser, and the file is yours to edit.



