配置多个 GIT 账号

配置多个 GIT 账号

生成 SSH 公钥

生成第二以及后面多个 sshkey

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ ssh-keygen -t rsa -C "test@test.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/xxx/.ssh/id_rsa): /c/Users/xxx/.ssh/id_rsa_coding #此处需要手动输入路径及公钥文件名,比如加上 coding,以表示用于 coding.net
Enter passphrase (empty for no passphrase): #可缺省
Enter same passphrase again: #可缺省
Your identification has been saved in /c/Users/xxx/.ssh/id_rsa_coding.
Your public key has been saved in /c/Users/xxx/.ssh/id_rsa_coding.pub. #公钥文件
The key fingerprint is:
SHA256:zAj36q123456/123456h6g8XReRdbQPLA4Psz123456 xxxxx@xxxxx.com
The key's randomart image is:
+---[RSA 2048]----+
| |
| o o+..oo|
| |
| o.= o o .. |
| |
| o*=E = + . |
| |
| ... =.o |
| |
+----[SHA256]-----+

查看公钥内容:

1
$ cat ~/.ssh/id_rsa_coding.pub

复制到 GITHUB 或者 Coding.net 中:

GitHub粘贴到 https://github.com/settings/ssh
Coding粘贴到 https://dev.tencent.com/user/account/setting/keys

config 文件

在存放公钥目录下生成 config 文件,填入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
# git account one
Host github
# Host git server
Hostname github.com
User test
IdentityFile ~/.ssh/id_rsa

# git account two
Host coding
# Host git server
Hostname git.coding.net
User test
IdentityFile ~/.ssh/id_rsa_coding

测试 SSH keys 密钥

根据 config 文件,输入命令

1
2
ssh -T git@github #域名换成设置的别名
ssh -T git@coding

出现如下提示即可。