git - 如何将一个本地 git 仓库推送到两个不同的 github 帐户

标签 git github ssh

在您将此标记为重复问题之前,请注意:

另一个问题似乎相关,但我相信它并不完全相同,并且发布的唯一答案是完全不够的。我尝试了 Shell Code 的解决方案,但无法使其工作: Two github accounts to push to same repo

另一个问题有类似的标题(@derek-brown 误导版本的结果),但问题实际上与我的完全不同: Pushing a local repo to multiple github accounts


这是场景:

本地存储库具有以下 Remote :

$ git remote -v
myremote1 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="02656b7642656b766a77602c616d6f" rel="noreferrer noopener nofollow">[email protected]</a>:github-user1/myproject.git (fetch)
myremote1 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f58564b7f58564b574a5d115c5052" rel="noreferrer noopener nofollow">[email protected]</a>:github-user1/myproject.git (push) 
myremote2 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="385f514c785f514c504d5a165b5755" rel="noreferrer noopener nofollow">[email protected]</a>:github-user2/myproject.git (fetch)
myremote2 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="01666875416668756974632f626e6c" rel="noreferrer noopener nofollow">[email protected]</a>:github-user2/myproject.git (push)

我希望能够以最简单的方式随意将此存储库推/pull 到两个 Remote 。

到目前为止,我已完成以下操作:

  1. 为两个身份创建 ssh key :

  2. 将身份添加到 ssh 代理:

$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_ed25519_github_user1
$ ssh-add ~/.ssh/id_ed25519_github_user1
  • 将公钥添加到相应 github 帐户的 SSH key 部分,如下所述:https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account

  • 在我的 ~.ssh 文件夹中添加了一个配置文件,其中包含以下内容:

  • #github-user1 account
    Host github-user1
      Hostname github.com
      User git
      IdentityFile ~/.ssh/id_ed25519_github_user1
    
    #github-user2 account
    Host github-user2
      Hostname github.com
      User git
      IdentityFile ~/.ssh/id_ed25519_github_user2
    

    当我尝试推送到任一 Remote 时,我收到如下错误:

    $ git push myremote1 main
    <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f28263b0f28263b273a2d612c2022" rel="noreferrer noopener nofollow">[email protected]</a>: Permission denied (publickey).
    fatal: Could not read from remote repository.      
    
    Please make sure you have the correct access rights
    and the repository exists.
    

    最佳答案

    好的,我通过更换 Remote 让它工作了。

    [版本清晰:我正在修改 .ssh/config 中声明的本地用户,以便它们与 github 用户不同,因为它们不必在 .ssh/config 中相同一般]

    考虑以下更新的 .ssh/config 文件:

    #github-user1 account
    Host local-user1
      Hostname github.com
      User git
      IdentityFile ~/.ssh/id_ed25519_github_user1
    
    #github-user2 account
    Host local-user2
      Hostname github.com
      User git
      IdentityFile ~/.ssh/id_ed25519_github_user2
    

    然后 Remote 更改为:

    $ git remote -v
    myremote1 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7c0ced3e7c0ced3cfd2c589c4c8ca" rel="noreferrer noopener nofollow">[email protected]</a>:github-user1/myproject.git (fetch)
    myremote1 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e5828c91a5828c918d9087cb868a88" rel="noreferrer noopener nofollow">[email protected]</a>:github-user1/myproject.git (push) 
    myremote2 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="13747a6753747a677b66713d707c7e" rel="noreferrer noopener nofollow">[email protected]</a>:github-user2/myproject.git (fetch)
    myremote2 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="56313f2216313f223e23347835393b" rel="noreferrer noopener nofollow">[email protected]</a>:github-user2/myproject.git (push)
    

    致:

    $ git remote -v
    myremote1 git@local-user1:github-user1/myproject.git (fetch)
    myremote1 git@local-user1:github-user1/myproject.git (push) 
    myremote2 git@local-user2:github-user2/myproject.git (fetch)
    myremote2 git@local-user2:github-user2/myproject.git (push)
    

    请注意,我已将“@”后面的主机名更改为我在 ~.ssh 文件夹的配置文件中输入的主机名。

    这就是对我有用的解决方案。

    但是,正如受人尊敬的@VonC所指出的,git@部分在 Remote 中不是必需的,因为git已经在~/.ssh/config中指定了 文件,作为用户 git。因此,我理解它们可能如下:

    $ git remote -v
    myremote1 local-user1:github-user1/myproject.git (fetch)
    myremote1 local-user1:github-user1/myproject.git (push) 
    myremote2 local-user2:github-user2/myproject.git (fetch)
    myremote2 local-user2:github-user2/myproject.git (push)
    

    我还没有测试过这个,但我相信它会起到同样的作用。

    关于git - 如何将一个本地 git 仓库推送到两个不同的 github 帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74739767/

    相关文章:

    email - 从SSH测试Zoho Mail服务器

    Visual Studio 上的 GIT 远程扩展 - 显示大量未知更改

    git - merge 没有 parent 的提交

    r - 如何从 R 脚本中将更改提交到 GitHub?

    git - 如何获取其他人的 pull 请求(修复它)

    java - sshj 使用 nohup 执行脚本

    python - 有没有办法在 Robot Framework 的 ssh 连接中使用 telnet?

    git - 在本地服务器和本地PC上使用GIT

    git - 显示不包含提交的分支

    git pull 失败并返回 "Either the application has not called WSAStartup, or WSAStartup failed"