简体   繁体   English

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

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

I made changes in my.env file and wrote after that in terminal git status :我在 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

I would like that the changes in the.env file be igonored by git so I added this line in.gitignore:我希望 git 忽略 .env 文件中的更改,所以我在.gitignore 中添加了这一行:

/.env

Now when I write git status I get the result:现在当我写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

What can I do now?我现在能做什么? Why is gitignore not ignored?为什么 gitignore 不被忽略?

The problem with a git rm --cached.env (which would enable to.gitignore to operate) is that, once committed and pushed, everybody lose the file on their next git pull . git rm --cached.env (这将使 to.gitignore 能够运行)的问题在于,一旦提交并推送,每个人都会在下一次git pull时丢失文件。

If you need to have local .env settings (that should not be part of your Git repository), you can either:如果您需要本地.env设置(不应该是 Git 存储库的一部分),您可以:

  • check if your.env comes with some kind of #include directive, which would allow you to include a second file.env.local (that you can add safely to your .gitignore )检查 your.env 是否带有某种#include指令,这将允许您包含第二个 file.env.local (您可以安全地添加到您的.gitignore
  • or setup a merge content driver, and version only .env.tpl (a template file), while ignoring .env itself (so using the git rm --cached.env , but with a git add .env.tpl )或设置合并内容驱动程序,仅版本.env.tpl (模板文件),同时忽略.env本身(因此使用git rm --cached.env ,但使用 git 添加.env.tpl

The idea behind a content filter driver is to generate automatically on git checkout / git switch the .env file, based on the .env.tpl template file and a local (and ignored) .env.values value file.内容过滤器驱动程序背后的想法是在git checkout / git switch .env文件时自动生成,基于.env.tpl模板文件和本地(和被忽略的) .env.values值文件。
See " Github private repo - manage packages for different environment " as an example of such an approach.请参阅“ Github 私人仓库 - 管理不同环境的包”作为这种方法的示例。

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

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