一 为个人账户生成密钥

生成文件:C:\Users\你的用户名.ssh\id_ed25519_personal(私钥)和.pub(公钥),按Enter三次(不设置密码,或自定义密码)
账号1
ssh-keygen -t ed25519 -C "[email protected]" -f "$HOME/.ssh/id_ed25519_b"
账号2
ssh-keygen -t ed25519 -C "[email protected]" -f "$HOME/.ssh/id_ed25519_3"
20260103132110379.png

二 将公钥添加到GitHub

1 复制个人公钥

$HOME/.ssh/id_ed25519_b.pub
全选复制输出内容(以ssh-ed25519开头)

2 添加到GitHub

登录个人GitHub账户 → Settings → SSH and GPG keys → New SSH key
Title填git_b,粘贴公钥 → Add SSH key
20260103132111223.png

3 账号2重复

$HOME/.ssh/id_ed25519_3.pub
登录工作GitHub,重复添加

三 配置SSH配置文件(C:\Users\你的用户名.ssh\config)

在Git Bash中创建/编辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
touch ~/.ssh/config
~/.ssh/config # 用VSCode打开编辑
或用记事本:notepad $HOME\.ssh\config。

添加内容(严格无BOM编码,用VSCode保存UTF-8):text
# 个人账户1
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_b
IdentitiesOnly yes

# 个人账户2
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_3
IdentitiesOnly yes
保存。右键config文件 > 属性 > 取消“只读”。

Windows SSH代理:首次运行Git时,可能需启动ssh-agent:

1
2
3
4
5
ssh-agent
ssh-add C:\Users\y\.ssh\id_ed25519_b
ssh-add C:\Users\y\.ssh\id_ed25519_3
![20260103132112101.png](https://imgs.032315.xyz/20260103132112101.png)
永久启动:在Windows服务中搜索“OpenSSH Authentication Agent” > 启动 > 自动 services.msc

20260103132112912.png

四 创建的三个文件

主全局配置文件(必须存在)
文件名:.gitconfig
完整路径:C:\Users\你的用户名.gitconfig
(如果还没有这个文件,Git 会自动创建,或者你手动创建)
个人账户专用配置文件
文件名:.gitconfig-b
推荐路径:C:\Users\你的用户名.gitconfig-personal
(和 .gitconfig 放在同一个目录,便于管理)
20260103132113704.png
工作账户专用配置文件
文件名:.gitconfig-3
推荐路径:C:\Users\你的用户名.gitconfig-work
(同样放在用户主目录下)
20260103132114483.png