工具/原料

ssh git

方法/步骤

制造第一把公钥:

ssh-keygen -t rsa -C "mywork@email.com"
# 设置名称为id_rsa_derek
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_derek
#添加到SSH agent中
ssh-add id_rsa_derek
制造第二把公钥:
ssh-keygen -t rsa -C "mywork@email.com"
# 设置名称为id_rsa_ranpop
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_ranpop
#添加到SSH agent中
ssh-add id_rsa_ranpop
将id_rsa_derek.pub添加到derek帐号的后台ssh。
将id_rsa_ranpop.pub添加到ranpop帐号的后台ssh。
在.ssh目录下配置config文件:
Host derek
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_derek
Host ranpop
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_ranpop
对于derek帐号下的仓库:
git clone derek:githubname/repository.git
(原地址是:git@github.com:githubname/repository.git,替换后应该是:derek:githubname/repository.git)
对于ranpop帐号下的仓库:
git clone ranpop::githubname/repository.git
(原地址是:git@github.com:githubname/repository.git,替换后应该是:ranpop:githubname/repository.git)
测试:
ssh -T derek
Hi derek! You've successfully authenticated, but GitHub does not provide shel l access.
ssh -T ranpop
Hi ranpop! You've successfully authenticated, but GitHub does not provide shel l access.
说明OK。
如果已经使用原地址克隆过了,可以使用命令修改:
git remote set-url origin derek:githubname/repository.git

相关原理:

1、一把公钥只能被一个GITHUB帐号拥有->因此必须为不同的帐号创建公钥。

2、交互时需要本地私钥与GITHUB帐号的公钥配对。

因此,要在同一台电脑上给两个属于不同帐号的仓库提交时,必须在本地创建两对公/私钥匙,分别把两把公钥给两个帐号,提交时根据要提交的仓库所属帐号选择相应的本地私钥即可;

当我们要在一个仓库上PUSH提交的内容时,使用以上的步骤可以选择对应的公钥,GITHUB服务器收到提交的内容时,先解析出仓库地址,然后从该仓库的所属帐号中找到一把能解锁该提交的公钥。