同时配置两个github账户

配置两个github账号,使用时互不干扰!

生成两个SSH key

生成两个key的具体命令:

1
2
ssh-keygen -t ed25519 -C "one@gmail.com"
ssh-keygen -t ed25519 -C "two@gmail.com"

注意在输入文件名时将两个文件进行区分

创建config文件并配置

继续在 ​​.ssh​​ 目录下创建 config 文件,在 config 文件中添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
# one(one@gmail.com)
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_one
User one

# two(two@gmail.com)
Host two.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_two
User two

部署 SSH key

将各自的ssh key写入到github账户

远程测试

1
2
ssh -T git@one.github.com
ssh -T git@two.github.com

运行命令后如果出现​​Hi xxxx! You’ve successfully authenticated, but GitHub does not provide shell access.​​时,恭喜你,配置成功了~

使用

1
2
git clone git@github.com: one的用户名/learngit.git
git clone git@two.github.com: two的用户名/learngit.git

如果设置了全局的email和username,此时提交到两个仓库的内容均为同一个user,如需设置不同的user,可以取消全局设置,仅在各个项目中设置user和email。

1
2
3
4
5
6
7
# 取消全局 用户名/邮箱 配置
git config --global --unset user.name
git config --global --unset user.email

# 单独为每个repo设置 用户名/邮箱
git config user.name "one_name" ; git config user.email "one_email"
git config user.name "two_name" ; git config user.email "two_email"