使用场景:使用公司邮箱往公司git服务器提交代码;使用个人邮箱往gitee上提交代码。
开始配置
1、生成两个不同的SSH
ssh-keygen -t rsa -C "<您的邮箱>"
Generating public/private rsa key pair.
Enter file in which to save the key (~/.ssh/id_rsa):<为了区分多个key,请填写自定义的名称>
说明
为了区分多个 SSH key,以上第3行请不要直接回车,请填写自定义的 key 路径名称以方便后续使用,如 ~/.ssh/603 和 ~/.ssh/company,然后点击回车确认。
代码演示:
使用公司邮箱生成第一个SSH:
ssh-keygen -t rsa -C "公司邮箱"
Generating public/private rsa key pair.
Enter file in which to save the key (~/.ssh/id_rsa): company
使用私人邮箱生成第二个SSH:
ssh-keygen -t rsa -C "私人邮箱"
Generating public/private rsa key pair.
Enter file in which to save the key (~/.ssh/id_rsa): 603
结果:在~\.ssh下生成了2个不同的ssh。一个是公司的,一个是个人的。
2、添加 SSH 密钥
将603.pub的内容粘贴到gitee的公钥里面;将company.pub的内容粘贴到公司git的公钥里面。
3、编辑~/.ssh/config文件(在~\.ssh下配置config文件)
#公司git
Host company
HostName 192.168.xx.xx
IdentityFile ~/.ssh/company
User xxx@公司邮箱.com
PreferredAuthentications publickey
IdentityAgent none
IdentitiesOnly yes
# gitee
Host 603
HostName gitee.com
IdentityFile ~/.ssh/603
PreferredAuthentications publickey
IdentityAgent none
IdentitiesOnly yes
说明
HostName:填写托管平台服务地址,红框内部分(这里很重要,非http地址,见下图);
Host:填写别名,方便命令行使用
IdentityFile:填写证书位置;
4、命令行操作
1)clone
假设公司代码克隆地址为:git@192.267.29.50:work/Codeup-Demo.git
个人代码克隆地址为:git@gitee.com:myname/Codeup-Demo.git