如何将代码推送到 Github 隐藏 API 密钥? [英] How to push code to Github hiding the API keys?

查看:70
本文介绍了如何将代码推送到 Github 隐藏 API 密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些代码推送到我的 GitHub 存储库.这些代码使用不同的语言,如 JavascriptJavaPython 等.其中一些代码包含一些私有的 API我不想发布的密钥.

I want to push some codes to my GitHub Repository. These codes are in different languages like Javascript, Java, Python etc. Some of those codes contain some private API key that I don't want to publish.

有没有办法自动隐藏密钥.?我应该手动从我的代码中删除它吗?

Is there any way to hide the keys automatically.? Should I remove it from my code manually.?

我想推送到 GitHub 的项目有很多.因此,手动删除不是一个好的选择.

There are many projects that I want to push to GitHub. So, manual removal is not a good option.

推荐答案

您应该考虑使用 .env 文件并从环境变量中读取密钥.如何操作取决于您使用的语言和工具(对于 node.jsphp 等).

You should consider using .env files and read the keys from the environmental variables. How to do so depends on the language and tools you use (for node.js, php, etc.).

您可以通过将 .env 添加到 .gitignore 来从提交中排除 .env 文件.您还可以上传带有虚拟数据或空白的示例配置 .env.example 以显示您的应用程序所需的架构.

You can exclude .env file from commits by adding .env to the .gitignore. You can also upload an example configuration .env.example with dummy data or blanks to show the schema your application requires.

这篇关于如何将代码推送到 Github 隐藏 API 密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆