同一台电脑配置多个 git 账户(github, gitee, gitlab等)
本地环境生成 ssh key
bash
cd ~/.ssh # 进入 .ssh 文件夹下
# 生成 key,邮件地址为 Gitee 或者 Github 使用的邮件地址
ssh-keygen -t rsa -C "[email protected]"
arduino
ssh-keygen -t rsa -C "Github 使用的邮件地址"
PS: 在Enter File in which to save the key (dir.../.ssh/id_rsa):
输入: id_rsa_github
同样配置gitee
的 ssh key
:
arduino
ssh-keygen -t rsa -C "Gitee 使用的邮件地址"
设置 Gitee
的 ssh key
为 id_rsa_gitee
完成后生成的文件大概是这样的:

配置 github, 将生成的公钥添加到 github 的 ssh key 中
先获取本地生成的 public key
bash
cd ~/.ssh
# 查看 id_rsa_github.pub 文件内容
cat id_rsa_github.pub ## 手动 copy 下获取到的内容
打开 github => 点击头像 -> Settings -> SSH and GPG keys

点击按钮, New SSH key
把刚刚 copy
的 github
的public key
粘贴, 添加一下title
, 点击确定就 OK 了
配置 gitee, 将生成的公钥添加到 gitee 的 ssh key 中
bash
# 查看 id_rsa_github.pub 文件内容
cat id_rsa_gitee.pub ## copy 内容
打开 Gitee
, 点击头像 -> 账号设置 -> SSH 公钥
把刚刚 copy
的 gitee
的public key
粘贴, 添加一下标题, 点击确定就 OK 了
创建配置文件, 解决 ssh 冲突
arduino
cd ~/.ssh
vim config // 创建并编辑 config
bash
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee
测试是否能正常连接
java
ssh -T [email protected] // successfully
ssh -T [email protected] // successfully