我有一个 Laravel 项目。根目录中有这4个文件:
.env .env.example .env.local .env.staging
我有一个 .gitignore 文件,我在 .gitignore 中依次列出这 4 个文件,就像这样
.env
.env.example
.env.local
.env.staging
我的 git 存储库不包含 .env 或 .env.example,但它包含 .env.local 和 .env.staging。 我已经尝试了我能想到的一切,但它一直在与 Gitlab 同步这两个文件。
有什么想法可能导致这种情况吗?
感谢您的帮助!
只需运行此命令即可。
git rm .env --cached
git commit -m "Stopped tracking .env File"
在 laravel 中包含用于连接数据库的 .env 文件。因此,您必须从缓存中删除 .env 文件。
您可以从缓存中删除该文件。
尝试以下命令
git rm .env --cached
git commit -m "Any message"
这是因为你的
.env
文件已经推送到git了。
您应该首先从 git 中删除它并推送您的更改。
rm -f .env
git add .
git commit -m "remove .env file from git"
git push
最好的解决方案是将
.env
文件放在它自己的文件夹中,然后将该文件夹添加到 .gitignore
文件中。
示例:
.env
文件移动到该文件夹中vars
中添加
.gitignore
*inside .gitignore*
/node_modules
/vars
-确保在使用
dotenv.config()
的主文件中添加
enviroment variables
的路径
dotenv.config({path: "./vars/.env"}
然后你可以像以前一样继续引用你的环境变量
更新 .gitignore 很简单:
*.env
*.env.example
*.env.local
*.env.staging
无需使用清除缓存命令,在 GUI 中也可以使用更简单的解决方案。
如果您绝对确定,您的
.gitignore
规则是正确的:
如果您认为尽管添加了文件名 gitignore 仍无法按预期工作,那么请确保先将其从 git 中删除。
对我来说,这是因为该文件已上演。我取消暂存 .env 文件,它被正确忽略。