Back to Blog
Security6 min readJanuary 15, 2024

Why You Should Never Commit .env Files to Git

One of the most common and dangerous mistakes developers make is accidentally committing environment variable files to version control. Here's why it matters and how to prevent it.

The Risk is Real

Every year, thousands of developers accidentally expose sensitive credentials by committing .env files to public repositories. In 2023 alone, security researchers found over 12 million leaked secrets on GitHub, many of which were database passwords, API keys, and authentication tokens stored in .env files.

The consequences can be severe:

  • Financial loss - Attackers can rack up massive bills on cloud services using your exposed AWS or GCP credentials
  • Data breaches - Database credentials can lead to complete data theft
  • Service compromise - API keys can be used to impersonate your application
  • Reputation damage - Users lose trust when their data is compromised
  • Legal liability - GDPR and other regulations impose significant fines for data breaches

How Secrets Get Exposed

There are several common scenarios where .env files end up in version control:

1. Forgetting to Add .gitignore

When starting a new project, it's easy to forget to create a .gitignore file or to add .env to an existing one. By the time you realize, the file might already be committed.

2. Creating .gitignore After Initial Commit

Adding .env to .gitignore doesn't remove it from Git history. If the file was committed before, it remains in the repository's history forever unless you rewrite history.

3. IDE Auto-commit Features

Some IDEs have features that automatically stage and commit all changes. Without proper .gitignore configuration, this can include sensitive files.

4. Copying from Examples

Developers sometimes copy a .env.example file to .env and fill in real values, forgetting that the .env file needs to be explicitly ignored.

Prevention Steps

1. Always Start with .gitignore

Make creating a comprehensive .gitignore the first step of any new project. Include all common .env file variants:

# Environment files
.env
.env.local
.env.*.local
.env.development
.env.production
.env.staging

2. Use Git Hooks

Set up a pre-commit hook to scan for potential secrets before they're committed. Tools like git-secrets, detect-secrets, or gitleaks can automatically block commits containing sensitive patterns.

# Install git-secrets
brew install git-secrets
# Configure for your repository
git secrets --install
git secrets --register-aws

3. Use .env.example

Always create a .env.example file with placeholder values that IS committed to the repository. This documents required variables without exposing actual secrets:

# .env.example - Safe to commit
DATABASE_URL=postgres://user:password@localhost:5432/dbname
API_KEY=your-api-key-here
JWT_SECRET=generate-a-random-string-at-least-32-chars

4. Regular Repository Audits

Periodically scan your repository for accidentally committed secrets using tools like truffleHog or GitHub's secret scanning feature:

# Scan entire Git history
trufflehog git file://path/to/repo

What to Do If You've Already Committed Secrets

If you discover that secrets have been committed to your repository, take immediate action:

  1. Rotate all exposed credentials immediately. This is the most critical step. Even if the repository is private, treat exposed secrets as compromised.
  2. Remove the secrets from Git history using git-filter-repo or BFG Repo-Cleaner:
    bfg --delete-files .env
    git reflog expire --expire=now --all
    git gc --prune=now --aggressive
  3. Force push to all branches (coordinate with your team first):
    git push --force --all
  4. Notify affected parties if user data may have been compromised.

Better Alternatives for Production

In production environments, avoid .env files entirely. Use your hosting platform's built-in secret management:

  • Vercel - Environment Variables in Project Settings
  • AWS - Secrets Manager or Parameter Store
  • Google Cloud - Secret Manager
  • Azure - Key Vault
  • Docker/Kubernetes - Docker Secrets or K8s Secrets
  • HashiCorp Vault - For enterprise-grade secret management

These solutions provide encryption at rest, access controls, audit logging, and automatic secret rotation - features that a plain .env file can never offer.

Key Takeaways

  • Never commit .env files to version control - the consequences can be severe
  • Always add .env patterns to .gitignore before your first commit
  • Use pre-commit hooks and secret scanning tools as additional safeguards
  • If secrets are exposed, rotate them immediately - don't just remove from history
  • Use proper secret management solutions in production environments