[英]How can I add a xml file from /res/values to my gitignore in my Android project?
I added a separate xml file to my /res/values folder which contains a secret api key that my app uses. 我在/ res / values文件夹中添加了一个单独的xml文件,其中包含我的应用程序使用的api密钥。
api_key.xml api_key.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="api_key" type="string">my_key</item>
</resources>
Now I would like to exclude it from git. 现在,我想将其从git中排除。 How can I do that?
我怎样才能做到这一点?
I added this line to the .gitignore
file in my project root 我将此行添加到项目根目录下的
.gitignore
文件中
/app/src/main/res/values/api_key.xml
but it does not work. 但它不起作用。 The file still appears on Github after pushing the project content.
推送项目内容后,该文件仍显示在Github上。
Git's ignore system only applies to files that aren't yet tracked. Git的忽略系统仅适用于尚未跟踪的文件。 Because you've already committed the file, ignoring it won't do anything.
因为您已经提交了文件,所以忽略它不会做任何事情。
First, and most importantly, invalidate the API secret key. 首先,也是最重要的是,使API密钥无效。
You've published it, and there is nothing you can do to make that key secure again. 您已经发布了它,您无法采取任何措施再次确保该密钥的安全。 Generate a new key to use going forward.
生成一个新密钥以供日后使用。 Do not skip this step.
不要跳过此步骤。 There are bots whose purpose in life is to look for leaked API keys on GitHub.
有些机器人的目的是在GitHub上查找泄漏的API密钥。
Remove the file from Git, probably using git rm --cached app/src/main/res/values/api_key.xml
from the root of your repository. 从存储库的根目录使用
git rm --cached app/src/main/res/values/api_key.xml
从Git删除文件。 This will remove the file from your local repository, but leave it in your local working copy. 这将从本地存储库中删除文件,但将其保留在本地工作副本中。 Because the file is no longer tracked, your ignore will now take effect.
由于不再跟踪文件,因此您的忽略现在生效。
Update the file with your new API key from step 1. 从步骤1开始,使用新的API密钥更新文件。
Optionally, add a placeholder file, eg app/src/main/res/values/api_key.xml.sample
, to your repository. (可选)将占位符文件(例如
app/src/main/res/values/api_key.xml.sample
)添加到存储库中。 This file should not include the API key, but rather should be a skeleton of the file you need, eg 该文件不应包含API密钥,而应是所需文件的框架,例如
<?xml version="1.0" encoding="utf-8"?> <resources> <item name="api_key" type="string">DUMMY_API_KEY</item> </resources>
Developers who work on the project should be encouraged to copy this file to to api_key.xml
and add a proper API key. 应鼓励从事该项目的开发人员将此文件复制到
api_key.xml
并添加适当的API密钥。
Push to GitHub. 推送至GitHub。 The existing file will be removed, and its history will remain in the repository.
现有文件将被删除,其历史记录将保留在存储库中。 That's okay, because you've invalidated the leaked API key.
没关系,因为您已使泄漏的API密钥无效。
You will have to delete it to remove it from github. 您必须删除它才能将其从github中删除。 The ignore just says "don't include any changes to things that match ..."
忽略只是说“不包括对匹配项的任何更改……”
This should do it: 应该这样做:
git rm --cache api_key.xml
This command says to remove the file from cache, but leave the file in place. 此命令说从高速缓存中删除文件,但将文件保留在原位。 I'm pretty sure this will tell Github to remove it from the current head.
我很确定这会告诉Github将其从当前头删除。 However, it will not remove the change history.
但是,它不会删除更改历史记录。
Back it up if there is any doubt. 如有任何疑问,请备份。 I'm not responsible ... (lol)
我不负责...(哈哈)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.