簡體   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