DoceAzedo
DoceAzedo

Reputation: 361

Should I commit an empty .env file?

I have a .env file storing my database information, and I'm about to publish my project on GitHub. So, my question is, should I commit an empty .env file with the keys that I need, but without the values so other people can understand how to fulfill it (and may later add it to the .gitignore file)?

For example, if my .env file looks like this:

DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3

Should I commit it like this or just don't commit it at all?

DB_HOST=
DB_USER=
DB_PASS=

Thanks in advance.

Upvotes: 6

Views: 3239

Answers (2)

Chuong Tran
Chuong Tran

Reputation: 360

You should commit, but with another name, like .env.example. With that empty env, another team member can know how the env looks like and can fill in by themself.

Upvotes: 9

Schwern
Schwern

Reputation: 165356

No. If you add a .env to Git, any future changes to .env will be seen by Git. They may be accidentally committed.

Adding a file Git is already tracking to .gitignore has no effect.

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected

For example, I've added and committed an empty .env and then modified it.

$ cat .gitignore 
.env

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   .env

no changes added to commit (use "git add" and/or "git commit -a")

$ git diff
diff --git a/.env b/.env
index 4a263e9..94d2d70 100644
--- a/.env
+++ b/.env
@@ -1,3 +1,3 @@
-DB_HOST=
-DB_USER=
-DB_PASS=
+DB_HOST=host
+DB_USER=user
+DB_PASS=pass

Add .env to your .gitignore and do not track it. Document your environment variables in your install and configuration documentation.

Upvotes: 3

Related Questions