Chapter 1 · Practice 1 of 5
Never commit secrets to git, even in private repositories
Why it matters
Git keeps every version of every file forever, and repositories get cloned, forked, made public and copied to laptops. A secret committed once should be treated as exposed, even if you delete it in the next commit, and even in a private repository.
Set up your project so real values never get near a commit:
- Keep real values in
.envfiles listed in.gitignore. The defaultcreate-next-apptemplate already ignores them. - Commit an
.env.examplewith the variable names and dummy values, so teammates know what to fill in. - Never add an exception that lets a real
.env.productionor similar file through.
To check, list what git tracks:
bash
git ls-files | grep -i env
Only the example file should appear.
Example
# .gitignore
.env
.env.*
!.env.example
# .env.example
DATABASE_URL=postgres://user:password@localhost:5432/app
STRIPE_SECRET_KEY=sk_test_replace_me