Git ERROR: Permission to user1/repoA.git denied to user2

Git ERROR: Permission to user1/repoA.git denied to user2

我有两个 github 帐户,我正在尝试配置 github、我的环境和 ssh 密钥,以便我能够推送到任何存储库,无论它是否在我的 github.com/user1 或 github.com/user2 帐户。我花了几个小时试图按照我能找到的所有资源来解决这个问题,唉,我似乎缺乏理解来解决这个问题。谁能帮我正确配置?

我在 github.com 上的两个帐户中的每一个都设置了不同的电子邮件地址; github.com/user1 = 'user1@user1.comgithub.com/user2 = user2@user2.com.

我有两个生成的 SSH 密钥,第一个在 .ssh/id_rsa.ssh/id_rsa.pub 中,这个 SSH 密钥保存在 github.com/user1 中。 .ssh/user2 中的第二个 SSH 密钥 .ssh/user2_rsaand saved ingithub.com/user1`.

我有一个 .ssh/config file 其中包含:

Host user1.github.com
 HostName github.com
 IdentityFile ~/.ssh/user1
Host user2.github.com
 HostName github.com
 IdentityFile ~/.ssh/id_rsa

当我 运行 ssh-add -l 我看到两个键都是 'loaded'.

当我 运行 ssh -T git@user1.github.com 我得到 Hi user2! You've successfully authenticated, but GitHub does not provide shell access.

当我 运行 ssh -T git@user2.github.com 我得到 Hi user2! You've successfully authenticated, but GitHub does not provide shell access.

我可以成功推送到 'github.com/user2' 中的一个仓库。

我无法推送到 'github.com/user1' 中的存储库,并显示以下错误消息;

ERROR: Permission to user1/repo.git denied to user2.
fatal: Could not read from remote repository.

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

我的 `.gitconfig' 文件包含:

[user]
    name = my full name 
    email = user1@user1.com
[GitHub]
   user = user1

据我所知,我的本地环境 cmd 似乎让我以 user2 的身份停留,并且不允许 user2 提交给 user1 github 帐户。但是 a) 我认为 .ssh/config 中的脚本应该认识到当我推送到 user1 时它应该使用 user1 ssh 反之亦然,但这似乎并没有发生 b) 我的 .gitcofig我的 user.email 和我的 GitHub 用户作为 user1,这与我在 运行ning ssh -T ....

之后被认证为 user2 冲突

我已经尝试了很多东西,现在我的内裤在这件上有适当的扭曲。谁能帮我正确设置这个,或者指导我如何删除所有 SSh 并重新开始并正确设置(请注意,我还设置了一个需要配置的 Heroku 帐户)。

提前致谢。

The 2nd SSH key in .ssh/user2 .ssh/user2_rsa

然而,您的 .ssh/configid_rsa 用于 user2.github.com

Host user2.github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa
                      ^^^^^^

并且user1.pub public 密钥似乎已在user2 帐户上注册,因为 ssh -T git@user1.github.com returns Hi user2!

检查您的本地配置,并在 github 端检查哪个 public 密钥已添加到哪个帐户。


注意:建议在.ssh/config文件中添加用户git

Host user2.github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa
  User git <=====

这样,URL 就变成了:

ssh -T user2.github.com

在 Linux 平台上

这个问题已经很好地解释了确切的问题是什么。 另外,@VonC 也巧妙地回答了这个问题。 我想补充一点信息,这可能会对像我这样的其他菜鸟有所帮助。

  • 首先,~/.ssh/目录下的config文件默认是不生成的。在我的例子中,默认情况下它不存在,但是可以通过导航到 .ssh 目录(或者也可以直接访问,无论你喜欢什么)并执行命令来轻松创建一个。

    nano config

然后简单的把上面问题中也提到的内容贴上去

Host user1.github.com
 HostName github.com
 IdentityFile ~/.ssh/user1
Host user2.github.com
 HostName github.com
 IdentityFile ~/.ssh/user2

我建议在创建像 'id_rsa' 这样的 SSH 密钥时不要使用密钥的默认名称。如果您在创建 SSH 密钥时没有指定任何名称,它会将其命名为 'id_rsa',然后当您试图回忆起该密钥是为哪个应用程序创建的时,它会在稍后造成混淆。最好将密钥重命名为 'github_user1' 或类似的名称(发挥您的想象力)。

之后,您可以使用 cmd

测试您的密钥
ssh -T git@user1.github.com

哪个应该return

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

同样适用于 user2。

  • 其次,如果系统仍然使用第一个用户的密钥,而 为第二个用户进行身份验证,这听起来可能很陈词滥调,但很简单 重新启动您的 linux 系统。每次我们启动系统并使用 SSH 密钥,os 缓存使用过的 SSH 密钥并重新启动系统 清除缓存。不过,您也可以使用 cmd 清除缓存。