1. 用Git生成两把钥匙;
#GitHub的钥匙
# kingboy @ KingBoydeMacBook-Pro in ~/.ssh [7:50:33]
➜ ssh-keygen -t rsa -C "kingboy@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/kingboy/.ssh/id_rsa): /Users/kingboy/.ssh/github_id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/kingboy/.ssh/github_id_rsa.
Your public key has been saved in /Users/kingboy/.ssh/github_id_rsa.pub.
The key fingerprint is:
SHA256:h6UQw+e68ncp5sidqbBpRk3WKUR04VgdJpeIlqWnfrc kingboyworld@163.com
The key's randomart image is:
+---[RSA 2048]----+
| +=+*++o |
| *Oo+o |
| o++o.. |
| ++++ |
| +.oS . |
| ..o . |
| .. ... .. |
| +=.+++o. |
| ooo===oE |
+----[SHA256]-----+
12345678910111213141516171819202122232425
#gitlab
➜ ssh-keygen -t rsa -C "personal@company.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/kingboy/.ssh/id_rsa): /Users/kingboy/.ssh/gitlab_id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/kingboy/.ssh/gitlab_id_rsa.
Your public key has been saved in /Users/kingboy/.ssh/gitlab_id_rsa.pub.
The key fingerprint is:
SHA256:h6UQw+e68ncp5sidqbBpRk3WKUR04VgdJpeIlqWnfrc personal@company.com
The key's randomart image is:
+---[RSA 2048]----+
| +=+*++o |
| *Oo+o |
| o++o.. |
| ++++ |
| +.oS . |
| ..o . |
| .. ... .. |
| +=.+++o. |
| ooo===oE |
+----[SHA256]-----+1234567891011121314151617181920212223
注意:
Enter file in which to save the key (/Users/kingboy/.ssh/id_rsa): /Users/kingboy/.ssh/github_id_rsa
输入的是钥匙的位置和名称。github和gitlab是不同的。
完成后会在~/.ssh/目录下生成以下文件:
github_id_rsa
github_id_rsa.pub
gitlab_id_rsa
gitlab_id_rsa.pub
将两个pub文件分别配置到github和gitlab的sshkey中
2. 编写config文件,告诉本地git到不同的国家带不同的钥匙。
例如:
github地址:github.com
gitlab地址:gitlab.max.com12
执行以下命令:
cd ~/.ssh
vim config12
config内容如下:(HostName根据自己实际需求来定)
#gitlab
Host gitlab
HostName gitlab.*.com
IdentityFile ~/.ssh/gitlab_id_rsa
#github
Host github
HostName github.com
IdentityFile ~/.ssh/github_id_rsa123456789
3. 配置仓库
例如:
github工作仓库:~/workspace/github
gitlab工作仓库:~/workspace/gitlab12
#gitlab
cd ~/workspace/gitlab
git init
git config --global user.name 'personal'
git config --global user.email 'personal@company.com'12345
#github
cd ~/workspace/github
git init
git config --local user.name 'kingboy'
git config --local user.email 'kingboy@163.com'12345
接下来在两个目录下新建或者clone项目开发即可.