git 多用户连接 gitee 和 github

一、通过邮箱生成公私钥(使用 git bash)

1
2
3
4
5
6
## git bash
# 生成 gitee 的公私钥
ssh-keygen -t rsa -f ~/.ssh/id_rsa_gitee -C "youxiang@qq.com"
# 生成 github 的公私钥
ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -C "youxiang@qq.com"
# 连按三下回车

image-20240221215650404

image-20240221215906227

二、公钥设置进网站账户

img

img

三、在 git bash 上激活公钥,并授权

1
2
3
4
5
6
7
8
9
10
## git bash
# 激活 gitee 公钥
ssh -T git@gitee.com -i ~/.ssh/id_rsa_gitee
# 授权
授权 控制台输入yes

# 激活 github 公钥
ssh -T git@github.com -i ~/.ssh/id_rsa_github
# 授权
授权 控制台输入yes

image-20240221220336995

若出现失败

  1. 首先查看 .ssh 目录下是否有 known_hosts 文件,若有,先删除
  2. 查看ssh链接的 github 地址
1
2
# 查看链接GitHub的地址
ssh -vT git@github.com

如果出现 127.0.0.1 或 ::1,说明 github 的 ip 地址访问不对,使用以下命令获取github的ip地址存入hosts文件

1
2
3
4
5
## 下面命令都是在 cmd 中执行
# 获取 github ip地址存入 hosts 文件
nslookup github.com 8.8.8.8
# 刷新 dns 缓存(windows)
ipconfig /flushdns

出现原因是用了 steam++ 修改了hosts

四、将私钥文件添加到 git

1
2
3
4
5
6
## git bash
# 启动链接输入
ssh-agent bash
# 私钥文件添加到 git
ssh-add ~/.ssh/id_rsa_gitee
ssh-add ~/.ssh/id_rsa_github

五、配置 config 文件

1
2
3
## git bash
#创建 config 文件,一般在 .ssh/config 文件内
touch ~/.ssh/config

文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
# config
Host gitee.com
HostName gitee.com
IdentityFile C:\\Users\\25361\\.ssh\\id_rsa_gitee
PreferredAuthentications publickey
User git


Host github.com
HostName github.com
IdentityFile C:\\Users\\25361\\.ssh\\id_rsa_github
PreferredAuthentications publickey
User git

测试如下,即为成功

image-20240221220935850