踩坑总结
网上关于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\公钥” 这样的转义反斜杆写法,但是也是可以用的。
官方解决方案
其实最靠谱还是官方文档,里面说的很是清楚,能少走很多弯路: