不知不觉就有了多个github帐户,平时为了方便都是采用ssh去操作git,所以这个时候问题就来了,在同一台机器上对多个github操作时,会因为ssh key只有一个而无法提交。所以我需要想办法配置多个ssh keys.
解决办法
放狗搜了一圈,有类似需求的大有人在。例如这里:Multiple GitHub Accounts & SSH Config
\’I\’m having some trouble getting two different SSH keys/GitHub accounts to play well together. I have the following setup:
Repos accessible from one account using git@github.com:accountname Repos accessible from another account using git@github.com:anotheraccount
傻瓜操作步骤
生成新的ssh key
ssh-add这一步很重要,否则是前功尽弃
ssh-keygen -t rsa -C \'work@mail.com\'
ssh-add ~/.ssh/work_rsa
配置.ssh/config
我只需要在~/.ssh/config里新增一个Host的别名,将不同帐号的区分开来就可以了。
Host me.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/me_rsa
Host work.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/work_rsa
配置git仓库
需要把git的配置更改过来,其中github.com更换为work.github.com,这样它会找到对应的key来登录。
git remote add origin git@work.github.com:work/test.git
One response to “多个github帐号的ssh keys支持”
非常感谢,再次在你的网站找到解决方案,我也需要类似的情况,几个git服务器的ssh证书不一样,输密码太麻烦了。