因为有两个 git 公司一个gitLab 自己一个github
解决方案是生成两套key,再生成一个配置文件
第一步:
将文件目录切换到.ssh
$ cd ~/.ssh
查看当前目录
$ ls
第二步:
生成两个ssh key
$ ssh-keygen -t rsa -C "XXX@XXX.XXX"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/XXX/.ssh/id_rsa): id_rsa
这里注意名称第二个key不要将第一个key覆盖
然后
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_test.
Your public key has been saved in id_rsa_test.pub.
The key fingerprint is:
SHA256:XXXXXXXXXX
XXX@XXX.XXX
The key's randomart image is:
+---[RSA 2048]----+
| X |
| X |
| X |
| X |
| X |
| X |
| X |
| X |
| X |
+----[SHA256]-----+
重复步骤2生成第二个key
$ ssh-keygen -t rsa -C "XXX@XXX.XXX"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/XXX/.ssh/id_rsa):id_ras_work
第三步:
$ vim id_rsa.pub
复制内容
把id_rsa.pub加到你的第一个账号上
$ vim id_rsa_work.pub
复制内容
把 id_rsa_work.pub加到你的第二个账号上
第四步:
把该key加到ssh agent上。由于不是使用默认的.ssh/id_rsa,所以你需要显示告诉ssh agent你的新key的位置
$ ssh-add ~/.ssh/id_rsa
$ ssh-add ~/.ssh/id_rsa_work
$ ssh-add -l 确认属否添加成功
第五步:
配置config
$ vim config
gitlab
Host gitlab
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa
github
Host github
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
通过$ cat config 来查看config是否配置成功