Safeguard
Security Guides•

Is it safe to commit a Laravel .env file to Git?

No. Laravel says to keep .env out of source control. Commit .env.example with placeholders, or use built-in environment encryption. Steps and cleanup inside.

Safeguard Security Team
3 min read

No. The Laravel documentation says the .env file should not be committed to source control, because anyone who gains access to the repository would get your credentials. Commit .env.example with placeholder values instead, or use Laravel's built-in environment file encryption if you must store it.

What the file holds

A default .env carries your APP_KEY, database credentials, mail and cache settings, and whatever API keys you add. The framework reads these values into configuration through the env function. Anything in .env can also be overridden by real server or system environment variables, which is the preferred place for production values.

The docs give two reasons not to commit it. Each developer and server may need different settings. And a repository leak, a compromised developer laptop or a forked public copy would expose live secrets.

Three safe patterns

PatternHow it worksGood for
.env.example committed, real .env per machinePlaceholders in Git, secrets set locallyMost teams
Platform environment variablesSecrets injected by the host or CIProduction
Encrypted environment fileLaravel encrypts the file so it can sit in GitTeams that want versioned config

The documentation notes that encrypted environment files may be placed in source control safely. The encryption key then becomes the secret to protect, so keep that outside the repository.

If a .env was already committed

Deleting the file in a new commit does not remove it from history. Treat the secrets as exposed.

  1. Rotate every credential in the file. Start with database users, third party API keys and the APP_KEY.
  2. For the APP_KEY, use the graceful rotation described in our post on rotating it.
  3. Add .env to .gitignore if it is not there.
  4. Remove the file from history with your Git host's recommended tool, then ask collaborators to re-clone.
  5. Check whether the repository was ever public or forked.
  6. Search logs of the affected services for use of the old credentials.

Rotation is the real fix. Rewriting history only reduces future exposure.

Prevent a repeat

  • Keep .env in .gitignore from the first commit.
  • Add a pre-commit or CI check that fails if a file named .env appears in a pull request.
  • Keep .env.example free of real values, and review it like code.
  • Make sure the web server document root is public, so .env is never reachable by URL.

A note on the web root

Laravel's default layout puts only the public directory behind the web server. Pointing the server at the project root would make files like .env fetchable. If you ever see a request for .env in your access logs, that is a bot looking for exactly this mistake.

Quick check

Is .env.example safe to commit?

Yes, as long as it contains placeholders only. The docs recommend it so teammates can see which variables are required.

Does encrypting the env file make committing it fine?

The docs say encrypted files may be placed in source control safely. Protect the decryption key separately.

Safeguard's asset discovery and compliance evidence automation can help you keep track of where your applications and their configuration live. A free scan is at gold.safeguard.sh/scan.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.