简体   繁体   English

如何从 Github 中隐藏.env 文件

[英]How to hide .env file from Github

I'm using dotenv for my rails project to store API keys.我在我的 rails 项目中使用 dotenv 来存储 API 密钥。 I have the dotenv gem in my gemfile, created a.env in the root of my app and have my API keys listed in there.我的 gemfile 中有 dotenv gem,在我的应用程序的根目录中创建了 a.env,并在其中列出了我的 API 密钥。 However, when I push up to github, I notice that the.env file is still there.但是,当我推到 github 时,我注意到 .env 文件仍然存在。 Am I missing a step?我错过了一步吗? How can I hide the.env file from being uploaded to github.如何隐藏 .env 文件不被上传到 github。

Thank you!谢谢!

Create a file called .gitignore in the root directory of your project ( it's possible one is already there ) and add .env on its own line in the file.在项目的根目录中创建一个名为.gitignore的文件(可能已经有一个文件),并在文件中.env的一行添加.env

Also, if you have already added it to your repository, then you will have to add the file to .gitignore as stated above and then clear Git's cache.此外,如果您已经将它添加到您的存储库中,那么您必须将文件添加到.gitignore如上所述,然后清除 Git 的缓存。 You can find information on that in the following answer on StackOverflow .您可以在 StackOverflow的以下答案中找到相关信息。

create a file.gitignore in your root directory and add.env file name in that.gitignore folder.After that You must untracked the.env file from git using git rm --cached.env and following that use 1. git add.在您的根目录中创建一个 file.gitignore 并在 that.gitignore 文件夹中添加.env 文件名。之后,您必须使用 git rm --cached.env 从 git 取消跟踪 .env 文件,然后使用 1. ZBA9F11ECC3497BDE169 2. git commit -m " " 3. git push -u origin master. 2. git commit -m " " 3. git push -u origin master。 Now your.env file is remove from github repo.现在 your.env 文件已从 github 存储库中删除。

您可以使用 .gitignore 文件从 repo 中排除文件: http : //git-scm.com/docs/gitignore

just add.env to the.gitignore file at the root of the project.只需将 .env 添加到项目根目录下的 .gitignore 文件即可。

if still, you can see the.env file in git changes, you need to prevent tracking this file from git by executing the below command如果仍然存在,您可以在 git 更改中看到 .env 文件,您需要通过执行以下命令来防止从 git 跟踪此文件

git rm --cached .env

In addition to this, the gitignore will not push your sensitive data to your version control that is why when you do an ls or when listing all the files that you have in your root folder git ignore doesn't appear unless you do ls --all when inside your gitignore files add the env file by typing.env inside the gitignore除此之外,gitignore 不会将您的敏感数据推送到您的版本控制,这就是为什么当您执行 ls 或列出根文件夹中的所有文件时 git 忽略不会出现,除非您执行 ls --在你的 gitignore 文件中通过在 gitignore 中键入 .env 添加 env 文件

If before you did git init and then some commit push your project.如果你之前做了 git init 然后一些提交推送你的项目。 now you realize that remove environmental variables for security purposes.现在您意识到出于安全目的删除环境变量。

1st try: create.gitignore in your root project then include your file which want to ignore in GitHub repository.第一次尝试:在您的根项目中创建.gitignore,然后在 GitHub 存储库中包含您想要忽略的文件。

if 1st one do not work,you can try this如果第一个不起作用,你可以试试这个

git rm --cached.env git rm --cached.env

git add. git 添加。

git commit -m "Removed.env" git 提交 -m "Removed.env"

git push git推

If u have already sent it to git hub, you can remove it by writing the following commands如果您已经将其发送到 git hub,您可以通过编写以下命令将其删除

git rm -r --cached .env

git add .
git commit -m 'your message'
git push

it will remove it automatically它会自动删除它

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

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