git 在终端上切换 Github 帐户时遇到问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7548158/
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
Having Trouble Switching Github accounts on terminal
提问by goddamnyouryan
It's been a while since I pushed anything to GitHub. I had initially set up my account on my computer, and everything worked great. Then I changed my account to a client's account (so I could push code to their private repository).
我已经有一段时间没有向 GitHub 推送任何内容了。我最初在我的电脑上设置了我的帐户,一切都很好。然后我将我的帐户更改为客户的帐户(因此我可以将代码推送到他们的私人存储库)。
It's been a while and now I am changing back to my old account, and I am having trouble. I generated a new rsa_key and pretty much followed the instructions hereto a T.
已经有一段时间了,现在我要改回旧帐户,但遇到了麻烦。我生成了一个新的 rsa_key 并且几乎按照此处的说明进行了操作。
However, when I type: ssh -T [email protected]
I get:
但是,当我输入:ssh -T [email protected]
我得到:
Hi oldincorrectusername! You've successfully authenticated, but GitHub does not provide shell access.
您好 oldincorrectusername!您已成功通过身份验证,但 GitHub 不提供 shell 访问。
I can't push to my repos either, because this old client username isn't authorized. I've doublechecked my ssh keys both on my computer and on my account setting on GitHub.
我也无法推送到我的存储库,因为这个旧的客户端用户名未经授权。我已经在我的计算机和 GitHub 上的帐户设置上仔细检查了我的 ssh 密钥。
I've also set my global account variables:
我还设置了我的全局帐户变量:
git config --global user.name "Firstname Lastname"
git config --global user.email "[email protected]"
git config --global github.user username
git config --global github.token 0123456789yourf0123456789token
And still it is giving me the old username.
它仍然给我旧的用户名。
Any suggestions?
有什么建议?
Thanks,
谢谢,
回答by Chris Johnsen
The problem is that your local sshis still offering your “old” SSH key to GitHub. This often comes up when you have one GitHub-recognized key (i.e. your “old” key) loaded in an ssh-agentbut want to use a different GitHub-recognized key (i.e. your “new” key).
问题是您的本地ssh仍在向 GitHub 提供您的“旧”SSH 密钥。当您在ssh-agent 中加载了一个 GitHub 识别的密钥(即您的“旧”密钥)但想要使用另一个 GitHub 识别的密钥(即您的“新”密钥)时,通常会出现这种情况。
sshoffers keys in this order:
ssh按以下顺序提供密钥:
- specified keys that have been loaded into the agent
- other keys that have been loaded into the agent
- specified keys that have not been loaded into the agent
- 已加载到代理中的指定密钥
- 已加载到代理中的其他密钥
- 尚未加载到代理中的指定密钥
By “specified keys” I mean those keys specified by the -i
command line option or the IdentityFile
configuration option (which can be given through ~/.ssh/config
or the -o
command line option).
“指定的键”是指由-i
命令行选项或IdentityFile
配置选项(可以通过~/.ssh/config
或-o
命令行选项给出)指定的那些键。
If your “old” key is loaded into the agent, but your “new” key is not, then sshwill always offer your “old” key (from the first or second categories) before your “new” key (only ever in the last category since it is not loaded), even when you specify your “new” key with -i
/IdentitiesOnly
.
如果您的“旧”密钥已加载到代理中,但您的“新”密钥未加载,那么ssh将始终在您的“新”密钥之前提供您的“旧”密钥(第一类或第二类)(仅在最后一个类别,因为它没有加载),即使你用-i
/指定了你的“新”键IdentitiesOnly
。
You can check which keys are loaded in your ssh-agentwith ssh-add -l
. If your “old” key is listed, then you can fix the problem by unloading it from your agent (be sure to also unload any other GitHub-recognized keys, except perhaps your “new” key):
您可以检查哪些键在被加载的ssh-agent用ssh-add -l
。如果列出了您的“旧”密钥,那么您可以通过从代理卸载它来解决问题(确保同时卸载任何其他 GitHub 识别的密钥,可能除了您的“新”密钥):
ssh-add -d ~/.ssh/old_key_file
If you are using Mac OS X, the system may be automatically loading your “old” key if you checked “Remember password in my keychain” when prompted for the password at one point; you can disable this automatic loading by deleting the Keychain entry for the key with the command/usr/bin/ssh-add -K -d ~/.ssh/old_key_file
. Other systems may do something similar, but the commands to tell them to “stop that” will be different.
如果您使用的是 Mac OS X,系统可能会在提示输入密码时选中“记住我的钥匙串中的密码”,系统可能会自动加载您的“旧”密钥;您可以通过使用命令删除密钥的钥匙串条目来禁用此自动加载/usr/bin/ssh-add -K -d ~/.ssh/old_key_file
。其他系统可能会做类似的事情,但告诉他们“停止”的命令会有所不同。
Instead of unloading the “old” key from your agent, you can set the IdentitiesOnly
configuration option to yes
, to tell sshto skip the second category of keys (non-specified agent-loaded keys). Your ~/.ssh/config
might include a section like this:
您可以将IdentitiesOnly
配置选项设置为 ,而不是从代理卸载“旧”密钥yes
,以告诉ssh跳过第二类密钥(非指定代理加载的密钥)。您~/.ssh/config
可能包括这样的部分:
Host github.com
User git
IdentityFile ~/.ssh/id_rsa # wherever your "new" key lives
IdentitiesOnly yes
This way, it will not matter whether any other GitHub-recognized keys are loaded into your agent; sshwill always offer only your “new” key.
这样,任何其他 GitHub 识别的密钥是否加载到您的代理中都无关紧要;ssh将始终仅提供您的“新”密钥。
If you anticipate needing to access the repositories of both GitHub accounts and you do not want to have to edit the configuration file whenever you want to switch between GitHub accounts, then you might setup your ~/.ssh/config
like this:
如果您预计需要访问两个 GitHub 帐户的存储库,并且不想在想要在 GitHub 帐户之间切换时编辑配置文件,那么您可以这样设置~/.ssh/config
:
Host clientname.github.com
HostName github.com
IdentityFile ~/.ssh/client_id_rsa # or wherever your "old" client key lives
Host github.com
IdentityFile ~/.ssh/id_rsa # or wherever your "new" key lives
Host github.com *.github.com
User git
Hostname github.com
IdentitiesOnly yes
Then use URLs like github.com:GitHubAccount/repository
for your repositories and URLs like clientname.github.com:GitHubAccount/repository
for your client's repositories (you can put the git@
prefix back in if you like, but it is not necessary since the above entries set the User
configuration variable).
然后将 URLgithub.com:GitHubAccount/repository
用于您的存储库,将 URLclientname.github.com:GitHubAccount/repository
用于客户端的存储库(git@
如果您愿意,可以将前缀放回原处,但没有必要,因为上述条目设置了User
配置变量)。