A good .gitignore file keeps generated output, machine-specific settings, caches, and local secrets out of your repository without hiding files that teammates need. This practical reference shows what to ignore by project type, how to combine language and toolchain rules, and how to remove files that Git already tracks safely.
Overview
A .gitignore file contains patterns that tell Git which untracked paths to leave out of normal status and staging workflows. It does not delete files, prevent a program from creating them, or automatically remove paths that were committed previously. Those distinctions matter when you are setting up a repository or auditing an existing one.
Start with a small, understandable file. Ignore outputs that can be recreated, private configuration that belongs outside version control, and settings that describe one developer's machine rather than the project. Keep source code, lockfiles, migration files, test fixtures, documentation, and required configuration examples visible unless the project has a specific reason to handle them differently.
Patterns can match a file or directory by name, use wildcards, and be scoped to a directory. A trailing slash makes the intention clear for directories. An exclamation mark can re-include a path that a broader rule excluded.
# A directory anywhere in the repository
.cache/
# A root-level environment file
/.env
# Ignore local variants but keep the example
.env.*
!.env.example
# Ignore generated output at the repository root
/dist/
/build/Do not treat a template as a complete answer. Combine rules for the language, framework, operating system, package manager, editor, and deployment process actually used by your project.
Checklist by scenario
Use the table as a starting checklist, then remove entries that your project deliberately commits.
| Project or source | Common patterns to consider | Double-check before ignoring |
|---|---|---|
| JavaScript and TypeScript | node_modules/, dist/, build/, .cache/, coverage output, framework caches such as .next/, .nuxt/, .svelte-kit/, or .turbo/ | Keep the package-manager lockfile used by the project, such as package-lock.json, yarn.lock, pnpm-lock.yaml, or another intentionally managed lockfile. |
| Python | __pycache__/, *.py[cod], .pytest_cache/, .mypy_cache/, .ruff_cache/, .coverage, htmlcov/, .venv/, venv/ | Decide whether environment files, notebook outputs, and distribution folders are reproducible or part of the project deliverable. |
| Java and JVM builds | *.class, target/ for Maven, build/ and .gradle/ for Gradle | Do not broadly ignore build configuration or wrapper files that developers need to reproduce the build. |
| Go | Compiled binaries, coverage output, profiling files, and local IDE metadata | Keep go.mod and go.sum when dependency checksums are part of the project workflow. Name binary patterns carefully so a source file is not hidden accidentally. |
| .NET | bin/, obj/, .vs/, local user settings such as *.user and *.suo | Keep solution files, project files, build props, and other shared configuration required by the team. |
| Android and mobile projects | Build output, local SDK configuration such as local.properties, generated intermediates, signing files, and IDE metadata | Never use a broad rule to hide source-controlled resources, manifests, provisioning configuration, or shared build settings. Treat signing material as sensitive. |
| Editors and IDEs | .idea/, *.iml, .vscode/, editor swap files, and workspace-specific settings | Some teams commit selected VS Code settings or shared IDE code-style files. Ignore individual workspace data, not automatically every configuration file. |
| Operating-system files | .DS_Store, Thumbs.db, desktop metadata, and temporary files | Use a global Git ignore file for personal operating-system noise when possible, while retaining project-specific rules in the repository. |
| Build and test artifacts | coverage/, reports/, generated documentation, temporary archives, and release output | Some generated files are published artifacts or checked-in fixtures. Confirm their role before excluding them. |
| Local secrets | .env, local override files, private keys, certificates, credentials, and tool-specific secret stores | Provide a safe example such as .env.example. Ignoring a secret does not undo a previous commit or rotate an exposed credential. |
A practical baseline
For a mixed web repository, a baseline might look like this:
# Dependencies and generated web output
node_modules/
dist/
build/
coverage/
# Local environment files
.env
.env.*
!.env.example
# Python caches, if the repository contains Python tooling
__pycache__/
.pytest_cache/
.venv/
# OS and editor noise
.DS_Store
Thumbs.db
.vscode/*
!.vscode/extensions.json
!.vscode/settings.jsonThis is intentionally conservative. The last two VS Code rules illustrate an important pattern: ignore a directory's contents, then re-include selected shared files. If the team does not share those files, remove the exceptions.
What to double-check
Lockfiles and dependency behavior
Package managers generate dependency directories, but their lockfiles usually describe a reproducible dependency tree and should be handled intentionally. Do not add a blanket rule such as *.lock without checking which lockfiles the project relies on. The same principle applies to language-specific dependency manifests and checksums.
Framework-specific directories
Modern frameworks often create caches and output directories with names that look similar to source directories. Verify whether a folder is disposable, generated during development, or required at deployment. When migrating frameworks, search for old cache names and remove rules that no longer apply.
Secrets and examples
Ignore local secret files, but commit documentation that explains required variables. A useful example file contains names and safe placeholder values, not copied production credentials. If a credential has already been committed, remove it from the repository history according to your team's incident process and replace or revoke it; changing .gitignore alone is not remediation.
Scope and precedence
Git can read ignore rules from the repository, nested directories, and a user's global configuration. A repository file should communicate what every contributor needs. Global rules are better for personal files, but they can make debugging confusing if a contributor forgets they exist. Use git check-ignore -v -- path/to/file to see which rule is responsible for an ignored path.
Generated files that are actually inputs
Some projects commit generated assets, API clients, documentation, or test snapshots because they are distributed or reviewed. The question is not whether a file was generated; it is whether the repository treats it as a required, reviewable project artifact. Document exceptions with a comment next to the rule.
Common mistakes
- Ignoring after tracking: Adding a pattern does not remove a file already in the index.
- Using overly broad names: A rule such as
build/orconfig/can hide an important source directory anywhere it matches. - Ignoring all environment files: This can hide a safe example that teammates need. Re-include an example file or use a narrower rule.
- Committing editor settings by accident: Decide whether settings are personal or shared instead of copying a generic IDE template unchanged.
- Ignoring dependency lockfiles: This can make installs and automated builds vary between machines.
- Assuming ignored means secure: An ignored file can still be copied, uploaded, or exposed through logs. Keep secrets out of commits and protect them through the appropriate secret-management workflow.
Remove an already-tracked file safely
First add or correct the ignore rule. Then inspect what Git currently tracks:
git status --short
git ls-files -- path/to/local-fileTo stop tracking one file while keeping it on your machine, use:
git rm --cached -- path/to/local-file
git add .gitignore
git commit -m "Stop tracking local file"For a tracked directory, use the recursive option:
git rm -r --cached -- path/to/generated-directory
git add .gitignoreReview the staged diff before committing. If you want to reapply the repository's ignore rules across the working tree, a commonly used approach is:
git rm -r --cached .
git add .
git statusThis changes the index, not the files in your working tree, but it can stage a large number of changes. Run it deliberately, inspect the result, and avoid using it as a substitute for understanding an unexpected deletion.
When to revisit
Review .gitignore whenever the repository adds a language, framework, package manager, editor, mobile toolchain, build system, or deployment platform. Revisit it during dependency or framework migrations, before creating a release branch, and whenever a commit unexpectedly contains generated output or local configuration.
A lightweight maintenance checklist is:
- Run
git status --short --ignoredand inspect both untracked and ignored paths. - Search the repository for build, cache, coverage, credential, and local-configuration directories.
- Confirm that source files, lockfiles, shared settings, examples, and required fixtures remain visible.
- Use
git check-ignore -vfor any surprising path. - Check staged changes before committing, especially after changing patterns or removing tracked files.
- Ask whether a generated artifact is disposable, reproducible, or intentionally distributed.
Keep the file readable with grouped comments and narrow patterns. A short explanation beside an exception can prevent a future cleanup from deleting a rule that the build depends on. Treat this checklist as a repository setup step and repeat it whenever the project's workflow changes.