清除 git 的全局设置

首先查看本地所有相关配置,避免影响

1
2
3
4
5
6
7
8
9
# 查看是否设置
git config --global --list

# 删除设置
git config --global --unset user.name
git config --global --unset user.email

# 查看配置来源(本地配置覆盖全局配置)
git config --list --show-origin

Windows 中配置

任意地方打开 cmd 控制台,输入以下内容:

ssh-keygen -t rsa -f %USERPROFILE%/.ssh/id_rsa_github -C "your_email@github.com"

  • id_rsa_github 为生成的文件名,需要与其他的区分开

  • your_email@github.com 为你在平台关联的邮箱,如:test@gmail.com

image-20260312111239573

配置多个 SSH 密钥

1
2
3
4
5
# 为 GitHub 生成 SSH 密钥
ssh-keygen -t rsa -f %USERPROFILE%/.ssh/id_rsa_github -C "your_email@github.com"

# 为 Gitee 生成 SSH 密钥
ssh-keygen -t rsa -f %USERPROFILE%/.ssh/id_rsa_gitee -C "your_email@gitee.com"

完成后会在 C:/Users/xxx/.ssh / 目录下生成以下文件

  • id_rsa_github
  • id_rsa_github.pub
  • id_rsa_gitee
  • id_rsa_gitee.pub

将生成的对应.pub文件打开,将数据拷贝添加到对应平台的SSH配置中(全量拷贝)

如:id_rsa_github.pub 中的全部内容,拷贝到github 的SSH配置中,如图所示:

image-20260312112124627

配置 SSH Config 文件

C:/Users/xxx/.ssh/config 文件中添加以下内容,以区分不同平台的密钥:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Gitee
Host gitee.com
HostName gitee.com
User git
IdentitiesOnly yes
# 此处的 id_rsa_gitee 要与之前ssh-keygen生成的对应,URL 也要正确才能找到文件
IdentityFile C:/Users/xxx/.ssh/id_rsa_gitee

# GitHub
Host github.com
AddKeysToAgent yes
IdentitiesOnly yes
PubkeyAuthentication yes
HostName github.com
User git
# 此处的 id_rsa_github 要与之前ssh-keygen生成的对应,URL 也要正确才能找到文件
IdentityFile C:/Users/xxx/.ssh/id_rsa_github

验证是否生效(在任意路径):

1
2
ssh -T git@gitee.com
ssh -T git@github.com

出现以下情况,表示成功,否则配置错误(IdentityFile需要绝对路径):

Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

配置文件权限

config 、id_rsa_github和 id_rsa_gitee 配置文件需要删除其他用户的权限,只保留管理员和当前用户

  1. 右键需配置的文件,查看属性,打开“安全”一栏

  2. 打开“高级” ,“禁用继承”

  3. 返回,在“安全”一栏的“组或用户名” 中,删除非当前用户和系统管理员的账号,如:Everyoneimage-20250824120719000

Linux 中配置

配置多个 SSH 密钥

1
2
3
4
5
# 为 GitHub 生成 SSH 密钥
ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -C "your_email@github.com"

# 为 Gitee 生成 SSH 密钥
ssh-keygen -t rsa -f ~/.ssh/id_rsa_gitee -C "your_email@gitee.com"

完成后会在~/.ssh / 目录下生成以下文件

  • id_rsa_github
  • id_rsa_github.pub
  • id_rsa_gitee
  • id_rsa_gitee.pub

将生成的对应.pub文件打开,将数据拷贝添加到对应平台的SSH配置中(全量拷贝)

如:id_rsa_github.pub 中的全部内容,拷贝到github 的SSH配置中

配置 SSH Config 文件

~/.ssh/config 文件中添加以下内容,以区分不同平台的密钥:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Gitee
Host gitee.com
HostName gitee.com
User git
IdentitiesOnly yes
IdentityFile ~/.ssh/id_rsa_gitee

# GitHub
Host github.com
AddKeysToAgent yes
IdentitiesOnly yes
PubkeyAuthentication yes
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github

验证是否生效(在任意路径):

1
2
ssh -T git@gitee.com
ssh -T git@github.com

出现以下情况,表示成功,否则配置错误(IdentityFile需要绝对路径):

Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

配置文件权限

config 、id_rsa_github和 id_rsa_gitee 配置文件需要删除其他用户的权限

chmod 644 ~/.ssh/config

同时推送多个远程仓库

在本地项目中,可以同时关联 Gitee 和 GitHub 的远程仓库:

1
2
3
4
5
6
7
8
9
10
# 添加 GitHub 远程仓库
git remote add github git@github.com:username/boot-demo.git
# 推送到 GitHub
git push github master


# 添加 Gitee 远程仓库
git remote add gitee git@gitee.com:username/boot-demo.git
# 推送到 Gitee
git push gitee master

仅推送单个远程仓库

1
2
3
4
# 如果只推送其中一个 github 仓库
git branch -M main
git remote add origin git@github.com:username/boot-demo.git
git push -u origin main

同时配置多个 Github 账号

详细教程和说明,可参考:博客

以 Linux 环境为例,再以上的基础上再添加一个 github 的账号配置

1
2
# 为 GitHub 生成 SSH 密钥
ssh-keygen -t rsa -f ~/.ssh/id_rsa_github_blog -C "your_email@github.com"

完成后会在~/.ssh / 目录下生成以下文件

  • id_rsa_github_blog
  • id_rsa_github_blog.pub

将生成的 id_rsa_github_blog.pub 文件打开,将数据拷贝添加到github平台的SSH配置中(全量拷贝)

~/.ssh/config 文件中添加以下内容(注意区分Host):

1
2
3
4
5
6
7
8
# GitHub
Host github_blog
AddKeysToAgent yes
IdentitiesOnly yes
PubkeyAuthentication yes
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github_blog

推送仓库

1
2
3
4
5
6
# 注意:修改为上面配置中对应的 Host,否则会定向到之前的配置中
git remote add blog git@github_blog:username/boot-demo.git
git push -u blog main

# 查看会显示存在三个仓库(github、gitee、blog)
git remote -v

SSH 连接异常(提交失败)

Git 提交时报错:

ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

用以下三个命令测试,均无法连接

1
2
3
ssh -T git@github.com
ping github.com
telnet github.com 22

解决方法

修改~/.ssh/config 配置,Port 改为 443,HostName 改为 ssh.github.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# GitHub
Host github.com
AddKeysToAgent yes
IdentitiesOnly yes
PubkeyAuthentication yes
HostName ssh.github.com
Port 443
User git
IdentityFile ~/.ssh/id_rsa_github

# GitHub
Host github_blog
AddKeysToAgent yes
IdentitiesOnly yes
PubkeyAuthentication yes
HostName ssh.github.com
Port 443
User git
IdentityFile ~/.ssh/id_rsa_github_blog