如何在 git 中管理用户友好的 .env?
How to manage a userfriendly .env in git?
我正在尝试将一个 .env 文件推送到我在 github 上的存储库中,该文件应该只包含没有任何数据的变量名称,因此其他人可以更轻松地使用 .env 进行配置。
之后,我将 .env
添加到我的 gitignore 文件中,但它会一直跟踪 .env 并在我提交推送时将其推送到我的存储库中。所以这是我的问题,你如何在不推送你的个人数据并覆盖它的情况下设法拥有一个 .env 文件?
有几种方法可以实现这一目标。
我认为最标准的方法是在您的存储库中有一个 .example
文件,然后该文件将针对项目的每个用户进行更改。所以你将只有一个 XYZ.env.example
并且你将在本地“实现”env.example
(我的意思是你将采用 .env.example
并进行有效的 .env
配置文件)。使用这种方法,您必须跟踪 XYZ.env.example
和 ignore XYZ.env
.
另一种方法是使用 git update-index --[no]-skip-worktree
.
来自文档:
Skip-worktree bit can be defined in one (long) sentence: When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead.
To elaborate, "reading" means checking for file existence, reading file attributes or file content. The working directory version may be present or absent. If present, its content may match against the index version or
not. Writing is not affected by this bit, content safety is still first priority. Note that Git can update working directory file, that is marked skip-worktree, if it is safe to do so (i.e. working directory version
matches index version)
这意味着您可以保持 初始 XYZ.env
文件没有 production/test/debug 值,然后您可以 自由地 更改它而不会 git 打扰您进行这些更改。
如果需要,您可以更新文件,并且可以推送新的更改。只需使用 git update-index --no-skip-worktree
.
取消标记文件
请记住,如果远程引入对 .env
的更改,那么您将在本地发生冲突(这是一件好事)。
最后一件事 - 此命令仅在本地有效。 每个 贡献者(如果he/she 希望)必须在他们的本地克隆上执行此操作。
我正在尝试将一个 .env 文件推送到我在 github 上的存储库中,该文件应该只包含没有任何数据的变量名称,因此其他人可以更轻松地使用 .env 进行配置。
之后,我将 .env
添加到我的 gitignore 文件中,但它会一直跟踪 .env 并在我提交推送时将其推送到我的存储库中。所以这是我的问题,你如何在不推送你的个人数据并覆盖它的情况下设法拥有一个 .env 文件?
有几种方法可以实现这一目标。
我认为最标准的方法是在您的存储库中有一个 .example
文件,然后该文件将针对项目的每个用户进行更改。所以你将只有一个 XYZ.env.example
并且你将在本地“实现”env.example
(我的意思是你将采用 .env.example
并进行有效的 .env
配置文件)。使用这种方法,您必须跟踪 XYZ.env.example
和 ignore XYZ.env
.
另一种方法是使用 git update-index --[no]-skip-worktree
.
来自文档:
Skip-worktree bit can be defined in one (long) sentence: When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead.
To elaborate, "reading" means checking for file existence, reading file attributes or file content. The working directory version may be present or absent. If present, its content may match against the index version or not. Writing is not affected by this bit, content safety is still first priority. Note that Git can update working directory file, that is marked skip-worktree, if it is safe to do so (i.e. working directory version matches index version)
这意味着您可以保持 初始 XYZ.env
文件没有 production/test/debug 值,然后您可以 自由地 更改它而不会 git 打扰您进行这些更改。
如果需要,您可以更新文件,并且可以推送新的更改。只需使用 git update-index --no-skip-worktree
.
请记住,如果远程引入对 .env
的更改,那么您将在本地发生冲突(这是一件好事)。
最后一件事 - 此命令仅在本地有效。 每个 贡献者(如果he/she 希望)必须在他们的本地克隆上执行此操作。