Skip to content
Discussion options

You must be logged in to vote

To prevent .env files from being pushed to GitHub, you should add them to your .gitignore file.

  1. Add .env to .gitignore

Open or create a .gitignore file in your project root and add:

.env

This tells Git to ignore the .env file.

  1. If .env was already committed

If the file was already committed, Git will keep tracking it even if it’s in .gitignore. You need to remove it from the repository cache.

Run:

git rm --cached .env

Then commit the change:

git commit -m "Remove .env from tracking"
git push

This removes the file from Git but keeps it on your local machine.

  1. Optional: Use .env.example

A good practice is to create a template file:

.env.example

This contains the variable names without s…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Prateek434
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Security Build security into your GitHub workflow with features to keep your codebase secure Question Ask and answer questions about GitHub features and usage Welcome 🎉 Used to greet and highlight first-time discussion participants. Welcome to the community!
3 participants