Do I need to commit .env files into the repository?

Viewed 14006

I have just started learning backend dev using django. My question is do I just commit the project files in the server folder alone, or should I also commit the .env folder to the repository?

I have done the following:

  1. I have created virtual environment and I have also installed django in venv.
  2. I have setup a django server and super admin.
  3. I have setup the config.json to protect my API key.
  4. Included the same in .gitignore.

What happens if I do or do not commit .env?

2 Answers

Assuming that your .env folder is your virtual environment, no you should not commit it.

The virtual environment should be rebuilt on the server using your requirements.txt file. The local environment you built on your development machine may have operating system specific binaries, and other compiled code that was generated for your local environment.

The server will have different compiled binaries, and therefore should rebuild the virtual environment using: pip install -r requirements.txt.

You shouldn't commit/include your .env file in your git repo because env stands for environment. You will different environment variables for your LOCAL, STAGING(development), PRODUCTION environments.

i.e. your LOCAL .env file might have something like

WEB_HOST=localhost
WEB_PORT=8000
ALLOWED_HOSTS=127.0.0.1, localhost

but your PRODUCTION .env will have something different like

WEB_HOST=www.mysite.com
WEB_PORT=8080
ALLOWED_HOSTS=www.mysite.com

Which is why you can't include your .env in your repo and should be created depending on the environment.

Related