多用户多账户GitHub的ssh设置

我有两个github账号,在同一台电脑上,一个用于个人,一个用于团队。如何使用同时使用ssh对github进行数据推送呢?

场景:

用户名1:jobs

用户名2:musk

仓库1:git@github.com:jobs/apple.git

仓库2:git@github.com:musk/tesla.git

ssh密钥1:~/.ssh/jobs

ssh密钥2:~/.ssh/musk

首先,假设你已经按照官方教程将jobs.pub和musk.pub分别加入两个账户的ssh公钥中。

在~/.ssh目录下找到config文件,如果没有就创建一个

1
touch config # 创建config

然后在config中写入如下信息

1
2
3
4
5
6
7
8
9
10
11
12
13
# 该文件用于配置私钥对应的服务器
# Default github user(first@mail.com)
Host jobs.com
HostName github.com
User git
IdentityFile C:/Users/Administrator/.ssh/jobs

# second user(second@mail.com)
# 建一个github别名,新建的帐号使用这个别名做克隆和更新
Host musk.com
HostName github.com
User git
IdentityFile C:/Users/Administrator/.ssh/musk

测试一下

1
2
3
4
ssh -T jobs.com
Hi jobs! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T musk.com
Hi musk! You've successfully authenticated, but GitHub does not provide shell access.

出现以上内容说明设置成功。

最后,如果用github进行发布hexo,deploy可以写为:

1
2
3
4
5
deploy:
type: git
repo: jobs.com/jobs.github.io.git
branch: master
message: