You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows10下GitHub主/工作账号SSH连接配置异常求助

问题描述

我有个人项目主账号personal@gmail.com,默认通过HTTPS连接。之后新增工作邮箱work@gmail.com,生成work和work.pub密钥并添加至工作GitHub账号,此前修改Git配置的用户名和邮箱均正常。现在希望让C:\Projects\Work路径下的所有项目通过SSH连接work@gmail.com账号,但遇到权限问题,报错显示用个人账号访问工作仓库被拒绝。

当前配置文件

1. C:\Users\MyUser\.ssh\config

# Main GitHub
Host main
    Hostname github.com
    User git
    IdentityFile ~/.ssh/main-github
    IdentitiesOnly yes

# Work GitHub
Host work
    Hostname github.com
    User git
    IdentityFile ~/.ssh/work_github
    IdentitiesOnly yes

2. C:\Users\MyUser\.gitconfig

[filter "lfs"]
    smudge = git-lfs smudge -- %f
    process = git-lfs filter-process
    required = true
    clean = git-lfs clean -- %f
[user]
    name = personal
    email = personal@gmail.com
[includeIf "gitdir:C:/Projects/Work/"]
    path = C:/Projects/work/.gitconfig
[core]
    editor = \"C:\\Users\\MyUser\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe\" --wait
    sshCommand = C:/Windows/System32/OpenSSH/ssh.exe

3. C:\Projects\Work\.gitconfig

[user]
    name = work
    email = work@gmail.com
[core]
    sshCommand = "ssh -i ~/.ssh/work_github"

测试情况

执行ssh -Tv main和ssh -Tv work均返回正常认证:

Hi main! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2192, received 2608 bytes, in 0.6 seconds
Bytes per second: sent 3664.8, received 4360.3
debug1: Exit status 1

Hi work! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2192, received 2608 bytes, in 0.6 seconds
Bytes per second: sent 3664.8, received 4360.3
debug1: Exit status 1

但工作目录项目执行Git操作时报错:

ERROR: Permission to work/project.git denied to personal.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.


解决方案

问题核心是工作目录的Git配置未正确加载,或项目Remote地址未匹配工作账号的SSH规则,按以下步骤修复:

1. 修正includeIf路径匹配规则

Windows下Git的gitdir匹配需要确保覆盖子目录,且路径大小写一致。修改根目录.gitconfig的includeIf:

[includeIf "gitdir:C:/Projects/Work/**"]
    path = C:/Projects/Work/.gitconfig

添加/**后缀确保匹配Work下所有子项目,同时修正路径大小写(原配置中work/.gitconfig改为Work/.gitconfig,保持和目录名一致)。

2. 复用.ssh/config的Host配置,简化SSH命令

修改C:\Projects\Work\.gitconfig的core.sshCommand,直接调用.ssh/config中定义的work主机,避免密钥路径出错:

[core]
    sshCommand = "ssh -F ~/.ssh/config work"

3. 更新项目的Remote地址

将工作目录项目的Remote地址从默认的git@github.com:work/project.git改为使用work主机的地址:

git remote set-url origin work:work/project.git

这里work对应.ssh/config中的Host名,Git会自动匹配对应的密钥和主机配置。

4. 验证配置生效

在工作目录项目下执行以下命令确认配置:

# 检查用户信息是否为工作账号
git config user.name
git config user.email

# 测试SSH连接是否使用工作账号
GIT_SSH_COMMAND="ssh -Tv" git fetch

若输出中显示Hi work!,则说明配置已生效。

额外排查点

  • 确保~/.ssh/work_github密钥文件权限正确:Windows下右键文件→属性→安全→高级,禁用继承并删除其他用户权限,仅保留当前用户的读取权限。
  • 清除SSH代理缓存:执行ssh-add -D,再重新添加工作密钥ssh-add ~/.ssh/work_github。

内容的提问来源于stack exchange,提问作者Hadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 16:32:49