How to Create a .gitignore File (2026)
A .gitignore file is one of the first things you should create in any new repository. Without it, Git tracks everything โ node_modules, compiled binaries, .env secrets, IDE configuration, OS junk files, and build artifacts. Your repository bloats, secrets leak, and collaborators deal with merge conflicts in files they never touch.
Pattern Syntax
Glob patterns: * matches any characters within a filename (*.log ignores all log files). ** matches across directories (**/test ignores test directories at any depth). ? matches a single character.
Directory trailing slash: build/ ignores the entire build directory and everything in it. Without the slash, build would match both files and directories named build.
Negation: ! re-includes a previously ignored pattern. If you ignore *.log but want to keep important.log, add !important.log after the *.log rule. Order matters โ negation must come after the ignore rule.
Language-Specific Ignore Rules
Node.js: always ignore node_modules/ (it can contain 100,000+ files), dist/ and build/ (compiled output), .env (secrets), *.log, and coverage/ (test coverage reports). node_modules alone can be hundreds of megabytes.
Python: ignore __pycache__/, *.pyc, *.pyo (compiled bytecode), venv/ or env/ (virtual environments), .egg-info/, dist/, build/, and .pytest_cache/. Virtual environments should be recreated from requirements.txt, never committed.
React: in addition to Node.js patterns, ignore .env.local, .env.development.local, .env.test.local, and .env.production.local. Create React App generates these environment-specific files locally.
Common .gitignore Mistakes
Adding .gitignore too late: if files are already tracked by Git, adding them to .gitignore does not untrack them. You need to run git rm --cached filename to remove them from tracking first.
Ignoring too much: if your .gitignore is overly aggressive, important files might be missing from the repository. After creating .gitignore, verify with git status that the right files are being tracked.
Not ignoring enough: IDE settings (.idea/, .vscode/), OS files (.DS_Store, Thumbs.db), and editor swap files (*.swp, *~) should always be ignored. These are machine-specific and cause unnecessary conflicts.
Global .gitignore for Your Machine
Your personal machine generates files that should never be in any repository: .DS_Store (macOS), Thumbs.db (Windows), .idea/ (JetBrains IDEs), .vscode/ (VS Code). Rather than adding these to every project, create a global .gitignore.
Set it up with git config --global core.excludesfile ~/.gitignore_global. Then add OS and IDE patterns to that file. This keeps project .gitignore files focused on project-specific patterns.
Global gitignore handles your environment. Project gitignore handles the project. This separation means team members using different OSes and editors do not need to coordinate their ignore rules.