Should I commit my .env file to GitHub?

No. 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 read 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