美文网首页程序员
github多账号多SSH操作神坑-MacOS

github多账号多SSH操作神坑-MacOS

作者: 三水nami | 来源:发表于2020-10-31 05:49 被阅读0次

因为开了两个github账号,需要两个SSH进行上传。在分别进行了SSH绑定之后。却一直显示permisson denied。接下来说下具体的坑和解决办法。

我的Github账号有两,姑且这么叫

  • usera
  • userb

usera是很久之前开的,userb是现在才开的。之前都用的usera,对应电脑上也是usera的SSH Key。这次git init之后,想传repo到userb上去。我给userb生成了SSH Key并上传到了对应账号,但Push时:

Permission to userb/blog.git denied to usera.

根据这一篇的表述,说是不同账号要设置不同的ssh config。具体来讲,是在.ssh/config中写入:

Host usera.github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/usera_rsa
    IdentitiesOnly yes

Host userb.github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/userb_id_rsa
    IdentitiesOnly yes

然后执行

ssh-add ~/.ssh/usera_rsa
ssh-add ~/.ssh/userb_rsa

最后改相应repo下的config

git config user.name "userb"
git config user.email "userb@gmail.com"

这么一大通操作后,我继续Push,又得到了

Permission to userb/blog.git denied to usera.

我:???

我怀疑是不是git config不对。执行了 git config --get [user.name]git config --get [user.email] 。返回的都是userb。我甚至改了全局的config,依然得到了

Permission to userb/blog.git denied to usera.

我又执行了 git config -l 看看,发现username有两行。前面的一行值为usera,后面一行为userb。然后我查看git config -e ,又确实只有一行我才设置的。get user.name也返回userb 。

然后我又查,stackoverflow上不少人遇到了这个问题。有人说,这是Xcode Cache的锅,自动存储了密码,去keychain里清除密码就好了。

我按步骤清了密码的,又得到了

Permission to userb/blog.git denied to usera.

很明显不是密码的问题。再说我都多久没用苹果自带的的钥匙串了,我快疯了,就没有一个方法可以让这个repo变成userb吗???

终于,看到有人说,强行添加Remote orgin的账号有用,这么试了一下

git config -e

强行加账号

remote="<https://userb@github.com/userb/blog.git>..."

然后输入密码,然后终于push成功了!

这个时候我又查看了一下git config

git config -l

发现user.name只剩一行了。

所以总结下来,最终解决都没用到SSH Key……问题也不在花了大把时间研究的SSH Key的config上,就是git config改不了用户的问题。我推测是两个github user只要有一个没权限,push就会有问题。之前也有个repo出现了两个user共同push的情况,但由于其中一个的邮件没有注册github账号,倒也出没什么问题。

至于为什么会同时存在两个user.name……现在也是个谜

另外,这样的情况据说这样也有用

git config --system --unset credential.helper

相关文章

网友评论

    本文标题:github多账号多SSH操作神坑-MacOS

    本文链接:https://www.haomeiwen.com/subject/zsojvktx.html