Why the .env matters so much and how to handle it properly

Published on 2026-09-12 by frfrand

The keys that leak most are uploaded by mistake. Three habits for handling .env properly and what to do when a credential has already reached a commit.

Why the .env matters so much and how to handle it properly

Short answer: the .env stores configuration that changes between environments, including secrets. Three habits prevent almost all leaks: ignore it in Git before the first commit, set permissions to 600, and version a .env.example without values. If a key has already reached a commit, it must be rotated: deleting it in a new commit is not enough.

The credentials that leak most are not stolen through a sophisticated attack. They are uploaded to a repository by mistake.

There are entire companies dedicated to scanning public repositories for keys. A cloud provider credential published by mistake can be detected and used in minutes, not days.

Why .env exists

The idea comes from an application design convention (the Twelve-Factor App methodology): configuration that changes between environments does not go in the code, it goes in the environment.

The same code must be able to run on your machine, in staging, and in production, changing only variables. This forces you to remove everything environment-specific from the code: connection strings, API keys, service addresses, signing secrets.

The .env file is simply a convenient way to load those variables when developing. In production, the file is often not even needed: the system running your service injects the variables.

The three habits

1. .gitignore before the first commit.

echo ".env" >> .gitignore

Order matters. If you add it later, the file is already in the history and removing it from there is much more work.

A check worth knowing:

git check-ignore -v .env

If it prints nothing, it is not ignored, no matter how much you think it is.

2. Permissions 600.

chmod 600 .env

Only your user reads it. On a server with multiple users or multiple services, this is not theoretical: it is what prevents another application's process from reading your credentials.

3. A versioned .env.example.

A file with all the keys and no values:

DATABASE_URL= STRIPE_SECRET_KEY= JWT_SECRET=

That one does go to the repository. It serves as living documentation of what configuration the project needs, and avoids the classic case of someone cloning the repository and losing two hours discovering which variables are missing.

If a key was already in a commit

Here is the most costly mistake, because it seems resolved and it is not.

Deleting the key from the file and making a new commit does not work. The key remains in the repository history. Anyone who has the repository, or who clones it if it is public, can recover it with a command.

The only thing that truly solves it is rotating the credential: generating a new one and revoking the old one.

Rewriting history (with git filter-repo or similar) may be desirable in addition, especially if the repository is public, but it is secondary. As long as the old credential remains valid, the problem exists.

Correct order: rotate first, clean up later.

A couple more things

Not every secret is an environment variable. For large teams or stricter requirements there are secret managers, with automatic rotation and access logging. For most projects, well-handled environment variables are enough.

Watch out for logs. It's easy, during a debugging session, to print the entire configuration and have it end up in a log file that is later shared.

Watch out for the frontend. Any variable that ends up in the package sent to the browser is public, even if it's called SECRET. If it has to be secret, it lives in the backend.

In ArduMaker, the environment variables of a deployment from GitHub are declared in its configuration, from the panel, separated into build and execution variables. Those with secret names are stored as references and not as values within that configuration. On the VPS they are written to files outside the repository that only the application user can read, and deploy logs show secrets masked.

Environment variables when publishing from Git

If your application uses DATABASE_URL or an API key, do not save those values in .env inside the repository. In production, configure the variables securely in the environment where the process runs and limit who can read them.

On an ArduMaker VPS you can manage deployment configuration and processes. Check which variables actually reach systemd, Docker, or your shell: not all environments automatically load the same files. If a credential is leaked, revoke and rotate it; deleting the file from the next commit does not remove exposure from the history.

Related: What are environment variables · What is a commit.

Frequently Asked Questions

Is the .env file uploaded to the repository? No. It is added to .gitignore before the first commit and only a .env.example with the variable names and no values is versioned.

What do I do if a key was already in a commit? Rotate it: generate a new one and revoke the old one. Rewriting history with git filter-repo can help, especially in a public repository, but it is secondary.

Is a variable with SECRET in the name secret in the frontend? No. Everything that ends up in the package sent to the browser is public, regardless of its name.

  • .env
  • secretos
  • seguridad
  • configuración
  • DevOps

Keep reading

All articles · VPS plans