繁体   English   中英

如何忽略 git 中的 .env 文件?

[英]How can I ignore the .env file in git?

我在 my.env 文件中进行了更改,然后在终端git status中写入:

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

我希望 git 忽略 .env 文件中的更改,所以我在.gitignore 中添加了这一行:

/.env

现在当我写git status我得到结果:

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
    modified:   .gitignore

我现在能做什么? 为什么 gitignore 不被忽略?

git rm --cached.env (这将使 to.gitignore 能够运行)的问题在于,一旦提交并推送,每个人都会在下一次git pull时丢失文件。

如果您需要本地.env设置(不应该是 Git 存储库的一部分),您可以:

  • 检查 your.env 是否带有某种#include指令,这将允许您包含第二个 file.env.local (您可以安全地添加到您的.gitignore
  • 或设置合并内容驱动程序,仅版本.env.tpl (模板文件),同时忽略.env本身(因此使用git rm --cached.env ,但使用 git 添加.env.tpl

内容过滤器驱动程序背后的想法是在git checkout / git switch .env文件时自动生成,基于.env.tpl模板文件和本地(和被忽略的) .env.values值文件。
请参阅“ Github 私人仓库 - 管理不同环境的包”作为这种方法的示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM