All posts

A Developer's Guide to .gitignore (with Ready-Made Templates)

June 9, 2026 · DevTools

git
workflow
developer-tools

A messy repository usually starts the same way: someone commits node_modules/, a .env full of secrets, or their editor's .idea/ folder. A good .gitignore prevents all of that. Here's how it works and how to get a complete one fast.

What .gitignore actually does

.gitignore tells Git which untracked files to ignore. It does not affect files already committed — for those you need git rm --cached. Each line is a pattern:

node_modules/      # ignore a directory
*.log              # ignore by extension
.env               # ignore a specific file
!.env.example      # but DON'T ignore this one

What you should almost always ignore

  • Dependencies: node_modules/, vendor/, .venv/
  • Build output: dist/, build/, .next/, target/
  • Secrets: .env, *.pem, credentials files
  • Editor/OS noise: .DS_Store, .idea/, .vscode/*, Thumbs.db

Secrets deserve special care. Even with .gitignore, if you've already committed a .env, it's in history. Rotate those keys. To share config safely, encrypt values with the Env Encoder and commit the encrypted version.

Patterns that trip people up

  • Trailing slash matters. build/ ignores a directory; build ignores files and directories named build.
  • Negation order matters. !file only works if the parent directory isn't already fully ignored.
  • Leading slash anchors to root. /config ignores only the top-level config, not nested ones.

Build a complete file in one click

Rather than copying snippets from a dozen Stack Overflow answers, pick your languages, frameworks, editors, and operating systems in the Gitignore Generator. It combines vetted templates — Node, Next.js, Python, Go, Rust, Java, macOS, Windows, JetBrains, VS Code, and more — into a single, de-duplicated file you can drop into your repo.

A sensible starting point for a Next.js app

# Node
node_modules/
*.tsbuildinfo

# Next.js
/.next/
/out/

# Environment
.env
.env.local

# macOS
.DS_Store

Generate yours now with the Gitignore Generator.

Tools mentioned in this post