.gitignore and pre-commit secrets setup
Pick your stack and get two things that stop keys from being committed: a .gitignore block for env files, private keys and credential files, and a ready-to-use pre-commit hook that scans every commit with gitleaks, detect-secrets or the Keysquid CLI. Generated in your browser.
.gitignore
# ── Secrets and environment (generated by keysquid.com/tools/gitignore-generator) ──
# Environment files: keep a committed .env.example with names only
.env
.env.*
!.env.example
!.env.sample
!.env.template
# Keys and certificates
*.pem
*.key
*.p8
*.p12
*.pfx
*.crt.key
id_rsa*
id_ed25519*
*.ppk
# Credential files
credentials.json
secrets.json
secrets.y*ml
*.secret
*.secrets
.netrc
.htpasswd
# Local tooling
.DS_Store
.idea/
.vscode/settings.json
*.log
# Node.js
.npmrc
.yarnrc.yml.local
*.local.json
# Next.js
.env*.local
.vercel/
.next/
.pre-commit-config.yaml
# Blocks commits that contain secrets. https://github.com/gitleaks/gitleaks
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.24.2 # check for the latest release tag
hooks:
- id: gitleaks
Set it up
pip install pre-commit # or: brew install pre-commit
pre-commit install
pre-commit run --all-files # scan what's already committedGenerated in your browser. A .gitignore only stops new files: if a secret was already committed, rotate it, because it stays in git history.
How it works
The .gitignore starts with rules every project needs (every .env variant except examples, *.pem, *.key, SSH keys and credential files), then adds the files your stack keeps secrets in: Rails master.key, Terraform *.tfvars and state, Firebase service-account JSON, .NET appsettings.Development.json, iOS and Android signing files, AI coding tool settings and more.
The pre-commit config runs a secret scanner on staged changes, so a commit with a key in it is blocked before it exists. Run it once over the whole repo too, to catch what’s already there.
FAQ
Isn't a .gitignore enough?
No. It only stops untracked files from being added. It doesn't help when a key is pasted into a source file, and it does nothing for files that are already committed. A pre-commit secret scanner catches the key in any file before it reaches the repo.
gitleaks or detect-secrets?
gitleaks is a single fast binary with good default rules and works in any repo. detect-secrets (Python) uses a baseline file, which suits big existing codebases where you need to accept old findings and block only new ones. Either is a big improvement over nothing.
Should I commit .env.example?
Yes. Commit a .env.example with the variable names and empty or fake values, so new developers know what to set. The generated .gitignore ignores every .env file except .env.example, .env.sample and .env.template.
I already committed a secret. What now?
Rotate it first: it is in git history and in every clone. Then remove it from the code, and optionally rewrite history with git filter-repo. Scan the repo with our GitHub secret scanner to find anything else.