bash 在 .gitconfig 中隐藏 GitHub 令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8505335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hiding GitHub token in .gitconfig
提问by gws
I would like to store all of my dotfiles on GitHub, including .gitconfig which requires me to hide the GitHub token in the .gitconfig.
我想将我所有的点文件存储在 GitHub 上,包括 .gitconfig,它要求我将 GitHub 令牌隐藏在 .gitconfig 中。
To do so I have a ".gitconfig-hidden-token" file which is the file I intend to edit and put under git that hides the token:
为此,我有一个“.gitconfig-hidden-token”文件,这是我打算编辑并放在隐藏令牌的 git 下的文件:
...
[github]
user = giuliop
token = --hidden--
...
And a shell script which I need to launch if I modify the ".gitconfig-hidden-token" file to create the ".gitconfig" file:
如果我修改“.gitconfig-hidden-token”文件以创建“.gitconfig”文件,我需要启动一个shell脚本:
cp .gitconfig .gitconfig.backup
sed 's/--hidden--/123456789/' .gitconfig-hidden-token > .gitconfig
The drawback is the need to manually launch the script everytime I modidy the file. Is there a better, fully automated way to do this?
缺点是每次修改文件时都需要手动启动脚本。有没有更好的,完全自动化的方法来做到这一点?
采纳答案by fge
Add your .gitconfig with git add -N.
添加您的 .gitconfig 与git add -N.
Then git add -pit, edit the hunk, replace the token with anything, and push that. No need for an extra file this way.
然后git add -p,编辑大块,用任何东西替换令牌,然后推送它。这样不需要额外的文件。
Addendum: on additional modifications of your file, use git add -pagain, and edit the hunk so that your initial manipulation not be overwritten.
附录:对您的文件进行其他修改,git add -p再次使用并编辑大块,以便您的初始操作不会被覆盖。
回答by Dave Dopson
I just fixed this up for myself. The "proper" way to solve the issue is to split your gitconfig into two files, a public one with the alias/config/etc, and a private file that keeps your username and secrets. Like so...
我只是为自己解决了这个问题。解决问题的“正确”方法是将您的 gitconfig 分成两个文件,一个带有别名/config/etc 的公共文件,以及一个保存您的用户名和机密的私有文件。像这样...
From https://github.com/ddopson/dotfiles...
从https://github.com/ddopson/dotfiles...
[include]
# For username / creds / etc
path = ~/.gitconfig.local
[alias]
...
.gitconfig.local:.gitconfig.local:[user]
user = ddopson
name = Dave Dopson
email = [email protected]
token = a123uber456secret789ceprivate000key78
[credential]
helper = osxkeychain
.gitignore:.gitignore:/.gitconfig.local
回答by idbrii
You can now include another file in your gitconfig. You could put your github section in that extra file. See this question: Is it possible to include a file in your .gitconfig
您现在可以在 gitconfig 中包含另一个文件。你可以把你的 github 部分放在那个额外的文件中。看到这个问题:Is it possible to include a file in your .gitconfig
回答by Dennis
I made a script to update my dotfiles repo, it also redacts sensitive information such as my github token. I don't think the github token is used by GitHub anymore though, but correct me if I'm wrong.
我制作了一个脚本来更新我的 dotfiles 存储库,它还编辑了我的 github 令牌等敏感信息。不过,我认为 GitHub 不再使用 github 令牌,但如果我错了,请纠正我。
You can view my script here.
你可以在这里查看我的脚本。

