Windows 下配置多个 GitHub 账号的 SSH Key
假设你有以下两个 SSH key 文件:
- 第一个账号:
id_rsa
(默认) - 第二个账号:
id_rsa_github
✅ 步骤:在 Windows 上配置多个 GitHub 账号 SSH Key
1️⃣ 打开 SSH 配置文件(没有就新建)
路径为:
C:\Users\Administrator\.ssh\config
如果没有这个文件,就用记事本新建一个名为
config
的无扩展名文件。
2️⃣ 编辑 config 文件,写入如下内容:
# 第一个 GitHub 账号(默认)
Host github.comHostName github.comUser gitIdentityFile C:/Users/Administrator/.ssh/id_rsaIdentitiesOnly yes# 第二个 GitHub 账号(别名为 github-second)
Host github-secondHostName github.comUser gitIdentityFile C:/Users/Administrator/.ssh/id_rsa_githubIdentitiesOnly yes
Windows 下路径中的
\
换成/
是 SSH 格式要求。
3️⃣ 克隆第二个账号的仓库用别名
git clone git@github-second:your-username/your-repo.git
4️⃣ 已有项目如何修改远程地址?
进入项目文件夹:
cd your-repo
git remote set-url origin git@github-second:your-username/your-repo.git
5️⃣ 测试 SSH 是否配置成功
打开 Git Bash 或 PowerShell,测试连接:
ssh -T git@github.com # 测试第一个账号
ssh -T git@github-second # 测试第二个账号
成功会提示:
Hi your-username! You've successfully authenticated, but GitHub does not provide shell access.
✅ 补充建议
设置 Git 用户(针对每个仓库):
git config user.name "你的用户名"
git config user.email "你的邮箱"
或全局设置:
git config --global user.name "你的默认用户名"
git config --global user.email "你的默认邮箱"