Unignored .env Environment File in Repository
Verifies whether local .env, .env.local, or .env.production files in your project folder are actually excluded by your root .gitignore before git add . stages them.
- Rule Slug
- unignored-dotenv-file
- Affected Files
- .env, .env.*
- Detection Method
- .gitignore Cross-Check
Vulnerable Pattern vs. Hardened Fix
# .gitignore (incomplete - misses .env.local and .env.production)
node_modules/
dist/
.DS_Store# .gitignore (ignores all env files while keeping safe example template)
node_modules/
dist/
.DS_Store
.env
.env.*
!.env.example.env
.env.*
!.env.exampleWhy This Fails in Production
A local .env file is the single most common repository-wide secret container in modern web development, aggregating database passwords, third-party API keys, OAuth client secrets, webhook signing keys, and encryption salts into one flat key-value file. Because teams rely on .env files locally while intending to keep them out of version control, a single missing or mistyped line in .gitignore turns routine commands like git add . or git commit -am into a catastrophic multi-credential leak.
Even teams that remember to ignore .env frequently overlook environment-specific variants such as .env.local, .env.production, .env.staging, or .env.development.local used by Next.js, Vite, Astro, and Laravel. When .gitignore only contains the exact entry .env instead of .env.*, those secondary environment files (often containing the production keys copied down while debugging an outage) are silently staged and pushed to GitHub or GitLab.
PushCheck cross-references every discovered .env* file against your actual parsed .gitignore rules (including negation patterns like !.env.example). If your .env file is properly ignored, PushCheck marks it as Safely Ignored instead of raising a false alarm. If it is NOT ignored, PushCheck raises a CRITICAL block so you can patch .gitignore before running git commit.
Best practice is to ignore all .env and .env.* variants by default while explicitly un-ignoring a committed .env.example template containing only empty keys or clearly documented dummy placeholders.
Remediation Steps
- 1Add .env, .env.*, and !.env.example to your root .gitignore file immediately.
- 2If the file was already tracked by Git, untrack it without deleting your local copy by running: git rm --cached .env .env.local
- 3If the .env file was previously pushed to a remote repository, treat every credential inside it as compromised and rotate all keys immediately.
- 4Commit a sanitized .env.example file with empty values so teammates know which environment variables are required.
Frequently Asked Questions
- Why does PushCheck check .gitignore instead of just warning on every .env file?
- Almost every local development folder legitimately contains a .env file. Flagging properly ignored .env files creates alert fatigue. By parsing your actual .gitignore rules locally, PushCheck only triggers a CRITICAL alert when a .env file would genuinely be staged by Git.
- Will adding .env to .gitignore remove an already-committed .env file?
- No. Git ignores new untracked files matching .gitignore, but continues tracking files already in the index. Run git rm --cached .env, commit the removal, and rotate any secrets if the file was pushed.
- Is .env.example flagged by this rule?
- No. Safe template filenames such as .env.example, .env.sample, and .env.template are explicitly whitelisted from the unignored-file error, though their contents are still scanned for real leaked secrets.