踩坑總結
網上關於git多賬戶配置的文章很多,但是其實有誤導,以如下配置來說吧:
# This is the account for author on github.com.
Host github.com
HostName github.com
User user1
IdentityFile ~/.ssh/id_rsa_github
PreferredAuthentications publickey
# This is the account for author on gitee.com.
Host gitee.com
HostName gitee.com
User user2
IdentityFile ~/.ssh/id_rsa_gitee
PreferredAuthentications publickey
# This is the account for author on git.d.com.
Host git.d.com
HostName git.d.com
User user3
IdentityFile ~/.ssh/id_ed25519_gitlab
PreferredAuthentications publickey
這個是一個支持三個git站點的拉取推送配置,這裏面的Host其實就是git@Host內容,譬如[email protected],[email protected] ,這個參數是最重要的,如果這個參數寫錯了,就可能導致如下的奇怪現象:
ssh -T [email protected] 是正常返回的,
git push 報錯,[email protected]: Permission denied (publickey).
這裏面的IdentityFile就是ssh公鑰生成的位置,這裏需要了解一下,windows10下可以使用斜槓和~,不需要使用 “C:\Users\Administrator\.ssh\公鑰” 這樣的轉義反斜杆寫法,但是也是可以用的。
官方解決方案
其實最靠譜還是官方文檔,裏面說的很是清楚,能少走很多彎路: