首页 > 解决方案 > 推送到 github 时 Git 未使用正确的 SSH 密钥

问题描述

所以我有两个不同的 github 帐户,我为它们创建了两个不同的 SSH 密钥id_meid_work但是当我尝试从我的工作帐户进行 git 推送时,它会使用我的个人 SSH 密钥对我进行身份验证。

这就是我所做的:

将每个 SSH 密钥添加到其各自的 github 帐户。然后按照步骤将它们添加到我的 SSH 代理:

ssh-add -K id_me
ssh-add -K id_work

并像这样设置我~/.ssh/config

# Personal github account
Host github.com
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_me

# Work github account
Host github.com-work
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_work

我正在尝试对我的工作帐户上的存储库进行一些更改。首先,我确保使用正确的命令克隆它:

git clone git@github.com-work:test.git

但是,当我将更改推送回来时,它会引发权限被拒绝错误消息:

ERROR: Permission to test.git denied to doctopus.
fatal: Could not read from remote repository.

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

另外,我注意到当我运行时ssh -T github.com-work,它会在我的个人帐户上对我进行身份验证:

Hi doctopus! You've successfully authenticated, but GitHub does not provide shell access.

所以问题看起来像 git 在推送到 github 时没有使用正确的 SSH 密钥。

标签: gitssh

解决方案


ssh -Tv github.com-work将显示实际使用的键。

这样,您可以验证您的 ~/.ssh/config 内容。

仔细检查注册到您的 GitHub 帐户的公钥的内容,以及两个公钥 (id_work.pubid_me.pub)的内容


推荐阅读