【问题标题】:Permission to user1/repo denied to user2, can't push to two accounts without changing settings each time对 user1/repo 的权限拒绝给 user2,每次不更改设置就无法推送到两个帐户
【发布时间】:2022-01-20 06:31:41
【问题描述】:

我见过一些类似的问题(实际上我已经尝试过解决我见过的每一个问题),但没有人能解决这个问题。本质上,我想推送到两个不同的 Github 帐户,而不是每次都更改我的配置文件。

我正在尝试从同一个系统推送到两个不同的 Github 帐户,都使用 SSH。我正在通过 VSCode 使用 WSL。 ssh -T 适用于我的两个 RSA 文件,两个 Github 帐户都有相应的 SSH 密钥,当我的 .ssh/config 文件对每个文件都有特定设置时,我可以推送到每个帐户。

我的.ssh/config 文件如下所示:

# Personal Account
Host github.com-user1
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa
    
# Work Account
Host github.com-user2
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_user2

按原样,推送到帐户 user1 工作正常,但尝试推送到帐户 user2 会出错:

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

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

但是,如果我将第二节 (#Work Account) 的第一行更改为 Host github.com,我可以突然推送到帐户 user2!我只是...无法推送到 user1。我得到了上述错误的相反,现在Permission to user1/repo.git denied to user2.

我试过了:
both 设置为 Host github.com,但这会让我回到原来的问题(只能作为 user1 推送。)

它似乎只是在它看到的.ssh/config 文件中选择第一节(同时优先考虑Host github.com),但我需要它来响应我从哪个帐户推送。

git config user.nameuser.email 被设置为每个 repo 的正确值。 User2 使用本地配置,而 User1 使用全局配置,但我认为这不是问题,因为我无需调整即可推送到两者。

我已将PreferredAuthentications publickeyIdentitiesOnly yes 添加到这两个节中,并且我在其他人的此文件配置中看到了其他一些内容,但没有一个更改得到输出的错误。

Control Panel > Manage Windows Credentials 没有与之关联的 GitHub 凭据(我检查过,没有,没有任何内容被更改/删除)

【问题讨论】:

    标签: github ssh ssh-config multiple-accounts


    【解决方案1】:

    ~/.ssh/config 中设置该节后,您需要更改每个远程名称以使用github.com-user1github.com-user2 作为SSH URL 中的主机名。这是让它可靠工作的唯一方法,您可以通过运行git remote set-url NEW-URL 来更改现有的远程(例如origin)URL。

    另请注意,您还需要在每个节中使用IdentitiesOnly yes 选项。

    这在the Git FAQ中有记录。

    【讨论】:

      猜你喜欢
      • 2012-03-25
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 2020-01-06
      • 2012-11-20
      • 2011-12-24
      • 2013-01-17
      • 2018-08-26
      相关资源
      最近更新 更多