ToolsWhisper

.env vs .env.example: What to Commit and What to Keep Out of Git

Commit .env.example and never .env. What each file is for, what belongs in the template, the .gitignore lines that work, and what to do after a leak.

Frank4 min read

Every project that reads configuration from environment variables ends up with two files that look nearly identical: .env and .env.example. They have opposite rules. One must never be committed, and the other should always be. Mixing them up is one of the most common ways credentials end up in a public repository.

What each file is

.env holds the real values for one environment: the database URL with its password, API keys, signing secrets. It belongs to a person or a server. It is listed in .gitignore, and it is never committed.

.env.example is a template. It lists every variable the application expects, with placeholder or empty values and no secrets, so a new developer can copy it to .env and fill it in. It is committed, and it is part of the documentation.

# .env.example  (committed)
APP_ENV=local
APP_URL=http://localhost:3000
DATABASE_URL=postgres://user:password@localhost:5432/app
STRIPE_SECRET_KEY=
JWT_SECRET=

# .env  (never committed)
APP_ENV=local
APP_URL=http://localhost:3000
DATABASE_URL=postgres://app:Xk3...real-password...@db.internal:5432/app
STRIPE_SECRET_KEY=sk_live_...
JWT_SECRET=...

What goes in the example

  • Every key the app reads, so the file is a complete list. A missing key is the usual reason "it works on my machine" fails for someone else.
  • Safe placeholders. An empty value, or an obviously fake one like changeme, or a local default such as http://localhost:3000.
  • Comments saying where to get a value or what format it takes.
  • Nothing real. Not an expired key, not a staging key, not a password that is "only used locally". Once it is in the history it can be found.

Keep the example current. The application reads .env, not the example, so a variable added in code but not in the template goes unnoticed until someone new clones the repository.

The .gitignore lines

.env
.env.*
!.env.example

The order matters. .env.* matches .env.local, .env.production and also .env.example, and the last line re-includes the example. Check it with git check-ignore .env.example: it should print nothing and exit with status 1, meaning the file is not ignored. (With -v git prints the negating pattern, which looks like a match but is not one.) git status should list .env.example as a new file and never .env.

The ignore rule does not untrack files

.gitignore only affects files that git is not already tracking. If .env was committed once, adding it to .gitignore later changes nothing. Check with git ls-files .env: any output means it is tracked. Stop tracking it, while keeping your local copy, with git rm --cached .env and commit the change. To find out whether it was ever committed, git log --all --full-history -- .env lists every commit that touched it.

Other env files

Frameworks add their own variants, and the rules differ, so read yours. As a general pattern:

  • Files meant for a single machine, usually named .env.local, are not committed.
  • Per-environment files such as .env.production hold values for a deployment and are normally kept out of git as well.
  • Some frameworks load a committed .env.development or .env.test containing only non-secret defaults, which is fine if it truly has none.

One caveat to know about: many front-end frameworks expose variables with a particular prefix, such as NEXT_PUBLIC_ or VITE_, to the browser by inlining them into the JavaScript bundle. A variable with that prefix is public by definition, so never put a secret in one.

Fail early when a variable is missing

The example documents what the app needs, so make the app enforce it:

const required = ["DATABASE_URL", "JWT_SECRET"];
const missing = required.filter((name) => !process.env[name]);
if (missing.length) throw new Error(`Missing environment variables: ${missing.join(", ")}`);

A clear error at startup is far better than a confusing failure halfway through the first request that happens to need the missing value.

Quoting and syntax

There is no single standard for .env files. dotenv libraries, Docker Compose and the shell each parse them slightly differently. Values containing spaces or a #, which many parsers treat as the start of a comment, should be quoted, and it is safest to write KEY=value with no spaces around the =. Test with the parser that will actually read the file.

Containers and production

Do not copy .env into a Docker image. List it in .dockerignore, because anything copied in becomes a layer that anyone who can pull the image can read. In production, prefer to inject real environment variables from your platform's secret store or your CI system over shipping a .env file to the server.

If a secret was committed

Deleting the file in a later commit does not help. Git keeps history, and so do forks, clones and caches.

  1. Rotate the secret first. Issue a new key or password and revoke the old one. This is the step that actually protects you.
  2. Then remove it from history if you want the repository clean, using git filter-repo or BFG Repo-Cleaner, and force-push. Treat that as housekeeping, not as the fix.
  3. Add a guard. A secret scanner such as gitleaks in a pre-commit hook or CI, and push protection on your hosting platform, catch the next one before it lands.

Generating both from one list

Writing the two files by hand is how they drift apart. The .env file generator takes one list of variables, lets you mark which are secret, and produces the real .env and a matching .env.example with the secrets blanked, so the template always lists exactly the same keys. If you need strong values for the secrets themselves, the random string generator makes them from the browser's cryptographic source.

Tools used in this article

Read next