You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

WSL环境下VSCode使用SSH推送至两个GitHub账户的权限问题求助

解决WSL中通过SSH同时推送至两个GitHub账户的权限问题

我之前也碰到过一模一样的问题!核心原因其实很简单:你虽然在~/.ssh/config里配置了两个不同的Host别名,但你的仓库远程地址(remote)还是用的默认的github.com,这就导致Git在连接时优先匹配了第一个Host规则(也就是你的user1账户),所以推送user2仓库时会提示权限被denied给user1。

下面是具体的解决步骤:

1. 为每个仓库修改远程地址

针对你的user2仓库,需要把它的remote URL换成你在config里定义的Host别名github.com-user2,而不是默认的github.com

打开user2仓库的终端,执行:

git remote set-url origin git@github.com-user2:user2/repo.git

(把user2/repo.git换成你实际的仓库路径)

同样,确认user1仓库的remote地址是:

git remote set-url origin git@github.com-user1:user1/repo.git

2. 验证远程地址是否正确

执行以下命令查看当前仓库的remote配置,确保地址里用的是你定义的Host别名:

git remote -v

正确的输出应该类似:

origin  git@github.com-user2:user2/repo.git (fetch)
origin  git@github.com-user2:user2/repo.git (push)

3. 优化你的SSH配置(可选但推荐)

在你的~/.ssh/config里给两个Host都加上IdentitiesOnly yes,这能强制SSH只使用你指定的密钥,避免系统自动尝试其他密钥(比如全局的id_rsa),配置如下:

# Personal Account
Host github.com-user1
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes

# Work Account
Host github.com-user2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_user2
IdentitiesOnly yes

4. 再次验证SSH连接

分别测试两个账户的SSH连接,确保都能成功:

ssh -T github.com-user1
ssh -T github.com-user2

成功的话会返回Hi [username]! You've successfully authenticated, but GitHub does not provide shell access.

额外注意点

  • 你已经设置的全局和本地user.name/user.email是正确的,这个配置影响的是commit的作者信息,和SSH权限验证无关,所以不用改动。
  • 确保Windows凭据里没有GitHub相关的缓存,你已经确认过这一点,很好。

这样修改之后,两个仓库就能分别用对应的SSH密钥推送,不会再出现权限混淆的问题了!

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

火山引擎 最新活动