视频加载失败

【工具】GitHub 多账号 SSH 配置完整指南

1567 字
8 分钟
【工具】GitHub 多账号 SSH 配置完整指南

前言#

很多人电脑上会有两个及以上的 GitHub 账号,比如一个是工作号、一个是个人号。默认的 SSH 配置只有一个私钥,当你用账号 A 的密钥去访问账号 B 的仓库时,GitHub 会认为你是账号 A,而账号 A 没有 B 仓库的权限,直接报”仓库不存在”。

这个问题的根源是:两个仓库用了同一个 SSH 地址 [email protected]。解决方法很简单——给每个账号单独配一套密钥,再用 .ssh/config 起个别名,B 的仓库 remote 改成别名地址就完事了。

第一步:生成两套独立的 SSH 密钥#

打开 Git Bash 或 PowerShell,执行以下命令:

Terminal window
# A 账号密钥(如果已有可以跳过)
ssh-keygen -t ed25519 -C "账号A的github邮箱" -f ~/.ssh/id_gh_a
# B 账号密钥(重点,新建这个)
ssh-keygen -t ed25519 -C "账号B的github邮箱" -f ~/.ssh/id_gh_b

提示输入 passphrase 时,可以直接回车用空密码,也可以设置密码(推荐)。

生成完毕后,把两个公钥分别添加到对应账号:

Terminal window
# 查看公钥内容
cat ~/.ssh/id_gh_a.pub
cat ~/.ssh/id_gh_b.pub

然后分别登录 GitHub:

  • 账号 A → Settings → SSH and GPG keys → New SSH key → 粘贴 id_gh_a.pub 内容
  • 账号 B → Settings → SSH and GPG keys → New SSH key → 粘贴 id_gh_b.pub 内容

第二步:创建 / 修改 ~/.ssh/config#

Windows 路径:C:\Users\你的用户名\.ssh\config

⚠️ 注意:文件名叫 config,不要加任何后缀(比如 .txt),编码用 UTF-8。

写入以下内容:

# GitHub 账号A
Host gh-a
HostName github.com
IdentityFile ~/.ssh/id_gh_a
User git
# GitHub 账号B
Host gh-b
HostName github.com
IdentityFile ~/.ssh/id_gh_b
User git

含义解释:

  • Host gh-a:给这个连接起个别名叫 gh-a,以后用这个别名代替 github.com
  • HostName github.com:实际连接的服务器
  • IdentityFile:使用哪个私钥
  • User git:SSH 连接 GitHub 使用的用户名(固定写 git)

第三步:测试连通性#

Terminal window
# 测试账号 A
ssh -T gh-a
# 测试账号 B
ssh -T gh-b

成功的话会看到类似这样的输出:

Hi B用户名! You've successfully authenticated, but GitHub does not provide shell access.

第四步:修改 B 仓库的 remote 地址#

进入账号 B 的本地仓库目录,先把当前的 remote 地址看一下:

Terminal window
git remote -v

原来大概率是 [email protected]:Bname/repo.git,这个会默认读取 id_gh_a,必须改成别名 gh-b:

Terminal window
git remote set-url origin gh-b:B用户名/仓库名.git

改完后验证一下:

Terminal window
git remote -v
# 应该看到 origin 用的是 gh-b:...

第五步:设置本仓库独立的提交作者#

现在 B 仓库用的密钥对了,但 commit 的 author 还是全局配置的 A 账号信息。用 --local 只在本仓库生效,不影响其他仓库:

Terminal window
git config --local user.name "B的GitHub用户名"
git config --local user.email "B的GitHub邮箱"

✅ --local 只作用当前仓库,不会污染 A 仓库的配置!

验证配置:

Terminal window
git config --local --list

第六步:以后的使用规范#

养成习惯,克隆的时候直接带上别名:

Terminal window
# 克隆 A 账号的仓库
git clone gh-a:A用户名/repo.git
# 克隆 B 账号的仓库
git clone gh-b:B用户名/repo.git

快速自检命令#

遇到问题先跑这三个,基本能定位:

Terminal window
# 看当前仓库 git 配置
git config --local --list
# 看远程地址
git remote -v
# 测试 B 账号 SSH 连通
ssh -T gh-b

常见报错:Permission denied (publickey) 排查#

遇到这个报错的核心原因是:SSH 找到了私钥,但 GitHub 拒绝了该公钥身份。最常见的根因是 remote 地址写错了。

高频坑:remote 地址写错了#

❌ 错误写法(把别名当成了用户名):

[email protected]:用户名/仓库名.git

gh-b 是你本地 config 里的 Host 别名,不是用户名。SSH 连接 GitHub 的固定用户名是 git,这个在 config 里已经写死了。

✅ 正确写法(直接用别名冒号分隔):

gh-b:用户名/仓库名.git

修正命令(进入仓库目录执行):

Terminal window
git remote set-url origin gh-b:B的GitHub用户名/仓库名.git

示例:B 账号名叫 wangbin247,仓库叫 demo:

Terminal window
git remote set-url origin gh-b:wangbin247/demo.git

按顺序排查(先测试 SSH 连通性)#

Terminal window
ssh -T gh-b

情况一:依旧返回 Permission denied (publickey)#

说明公钥没有正确添加到 B 账号。按以下步骤检查:

  1. 完整复制公钥内容(以 ssh-ed25519 开头,结尾是邮箱):
    Terminal window
    cat ~/.ssh/id_gh_b.pub
  2. 登录 B 账号 GitHub 网页 → Settings → SSH and GPG keys → New SSH key
  3. 粘贴进去,标题随便写,添加

⚠️ 公钥必须加到 B 账号,不要加到 A 账号!

情况二:返回成功 Hi B用户名! You've successfully authenticated...#

SSH 连通正常,问题在 git remote 地址,按下面的优先级检查:

  1. 确认 config 内容正确(Host 名称、IdentityFile 路径):
    Host gh-b
    HostName github.com
    IdentityFile ~/.ssh/id_gh_b
    User git
  2. 确认 remote 地址格式正确(见上面的正确写法)
  3. 确认 B 账号对该仓库有读写权限(私有仓库必须是 B 创建或被邀请为协作者)
  4. 确认公钥已添加到 B 账号(见情况一的步骤)

排查顺序总结#

ssh -T gh-b 失败 → 检查公钥是否添加到了 B 账号
ssh -T gh-b 成功 → 检查 git remote 地址格式是否正确

常见问题#

Q:已经 push 到远程的历史 commit 作者是 A,需要改吗?

不建议改。已 push 的历史 commit 重写作者会破坏协作,后续新提交用 B 就行。

Q:两个仓库能不能共用同一个 SSH 地址?

不能。这正是踩坑的根源,每个账号必须用不同的别名地址。

Q:ssh -T gh-b 返回权限被拒绝怎么办?

参考上面的【常见报错排查】,按顺序检查:公钥是否添加到 B 账号 → config 是否正确 → remote 地址格式是否正确。

总结#

步骤操作关键命令
生成密钥为每个账号单独生成一对密钥ssh-keygen -t ed25519 -f ~/.ssh/id_gh_x
配置别名编辑 ~/.ssh/config 设置 HostHost gh-b → IdentityFile ~/.ssh/id_gh_b
修改 remoteB 仓库 remote 改用别名地址git remote set-url origin gh-b:user/repo.git
设置本地作者仅当前仓库生效git config --local user.name "B用户名"

整个流程其实就四步:生成密钥 → 配 config → 改 remote → 设 local 作者。记住核心原则:每个账号一套密钥,每个仓库一个别名,就不会再踩坑了。

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!

打赏
【工具】GitHub 多账号 SSH 配置完整指南
https://blog.huhaha.vip/posts/github-multi-account-ssh-config/
作者
小王的博客
发布于
2026-10-13
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
小王的博客
一个上了年纪的猿人.
分类
标签
最新动态
站点统计
文章
210
分类
15
标签
162
总字数
481,598
运行时长
0 天
最后活动
0 天前
站点信息
构建平台
Netlify CI
博客版本
Firefly v6.16.8
文章许可
CC BY-NC-SA 4.0
文章目录