Should I commit my .env file to GitHub?

Published 2026-11-16 by Nordik. Written with AI from the sources listed at the end.

Short answerNo. Keep .env files (settings files that hold your keys) out of your repository.

A .env file is a settings file where an app keeps its keys, which are long passwords it uses to prove who it is to other services. To commit is to save a change into your repository, the folder that holds your app's code and its history. The Next.js documentation notes that the default project template adds .env files to .gitignore, and that you almost never want to commit them.

GitHub secret scanning looks through your Git history for credentials and alerts you, but prevention is simpler. If a secret was committed, replace it: make a new one and turn the old one off. GitHub says to rotate an affected credential immediately.

A quick check: open your repository on GitHub and search for the file name .env. If it shows up, replace every key inside it, add the file name to .gitignore, and stop tracking it so it does not come back with the next save.

Sources

Each source below was accessed on 2026-11-16. Documentation changes over time, so check the source for the current wording.

  • Next.js documentation, Environment variables, accessed 2026-11-16
  • GitHub documentation, About secret scanning, accessed 2026-11-16

See what your live app shows strangers

Paste the link to your app: in under a minute the free scan shows what your live app hands to any visitor, and where to look first. No sign-up, and it never signs in or changes anything.

Run a free scan

Want the code itself read? Order the $49 audit or see a sample report. Your card is held and charged only when your report is delivered. It is not a pentest: the audit is done by AI, and a second AI checks each serious finding.