github多賬號多SSH操作遇到的權限問題-MacOS

因為開了兩個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 github.com-usera
    HostName github.com
    User usera
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/usera_rsa
    IdentitiesOnly yes

Host github.com-userb
    HostName github.com
    User userb
    PreferredAuthentications publickey
    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沒有生效

其實這是個非常低級的錯誤。因為我使用的遠程倉庫是http協議(https://github...),不是SSH協議(git@github.com)。當然改SSH是沒有用的。

使用http協議,在使用多個賬戶需要加上這個

git config --global credential.github.com.useHttpPath true

即便是使用ssh協議,在執行上一節的步驟后,也還需要在git config -e里編譯remoteURL,與ssh config中的Host值對應。

[remote "origin"]
url=git@github.com-userb:userb/xxx.git

參考:

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。