我应该提交一个空的 .env 文件吗?

Should I commit an empty .env file?

我有一个 .env 文件存储我的数据库信息,我即将在 GitHub 上发布我的项目。所以,我的问题是,我是否应该提交一个空的 .env 文件,其中包含我需要的键,但没有值,以便其他人可以理解如何实现它(并可能在以后添加它到 .gitignore 文件)?

例如,如果我的 .env 文件如下所示:

DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3

我应该这样提交还是根本不提交?

DB_HOST=
DB_USER=
DB_PASS=

提前致谢。

没有。如果您将 .env 添加到 Git,以后对 .env 的任何更改都会被 Git 看到。它们可能是不小心犯的。

添加文件 Git 已经跟踪到 .gitignore 没有效果。

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

例如,我添加并提交了一个空的 .env 然后修改了它。

$ 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

.env 添加到您的 .gitignore 并且不要跟踪它。在安装和配置文档中记录您的环境变量。

您应该提交,但使用另一个名称,例如 .env.example。 有了那个空的环境,其他团队成员就可以知道环境的样子并可以自己填写。