【问题标题】:What steps must I take to consistently push to two Github accounts using SSH?我必须采取哪些步骤才能使用 SSH 持续推送到两个 Github 帐户?
【发布时间】:2017-07-21 02:25:17
【问题描述】:

我承诺在 Github 上工作和个人使用。在过去的两个月中,每次切换 github 上下文时,我都会遇到麻烦。每天晚上(和每天早上)我都会像这样在 Github 上推送或拉取错误:

ERROR: Permission to AlexanderBollbach/test.git denied to alexbollbach.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights

如果我可以让推/拉正常工作(通常是通过一些迂回的路径阅读有关 SSH/Github/Git 的 StackOverflow 帖子并且没有深入理解它们),当我切换时我会立即再次遇到同样的问题从工作 Github 到家,反之亦然。

我注意到的唯一有趣的线索是,在denied to alexbollbach 中,“alexbollbach”是我工作的 Github 帐户用户名。嗯,这让我考虑~/.ssh/config,其内容是:

Host *
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/id_rsa

Host github.com
 User git
Host helper
    HostName <work-domain-stuff>
    IdentityFile ~/.ssh/eng_rsa
    User eng

有人告诉我这个文件有助于配置 git 的 ssh 尝试。但是我仍然无法在这个文件和 git push 错误之间建立联系,认为我是alexbollbach。事实上,我不知道 ssh、我的密钥对和配置文件与 Github 用户的概念有什么关系。

请帮忙。最好不仅仅是修复,而是对我没有掌握的内容进行一些澄清。我通常每周至少两次花费一个小时来解决这个问题。

【问题讨论】:

    标签: git github ssh


    【解决方案1】:

    在 Git 中链接远程 repo 和不同 ssh 密钥的方式是通过远程 url。
    像这样更改这些网址:

     cd /path/to/my/work/repo
     git remote set-url origin workgh:myWorkLogin/myWorkrepo.git
    
     cd /path/to/my/perso/repo
     git remote set-url origin persgh:myPersoLogin/myPersorepo.git
    

    请参阅“How to work on personal GitHub repo from office computer whose SSH key is already added to a work related GitHub account?”作为完整示例。
    您的 ~/.ssh/config 将为每个 url 引用正确的 ssh 密钥:

    # Personal GitHub
    Host persgh
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_perso
    
    # Work GitHub
    Host workgh
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_work
    

    不要使用“github.com”作为“主机”:为主机键使用特殊名称更明确,表明这是一个 ssh url,您必须通过 ~/.ssh/config 文件解析。

    【讨论】:

    • 你能解释一下“workgh:myWorkLogin”吗?这种配置是否允许我与来自我的任何一个 github 帐户/组织的 repos 上的任意分支进行稳健的通信?每次在我的任一上下文(工作/个人)中与任何远程存储库同步时,我是否需要特别注意配置远程?
    • @AlexBollbach 抱歉,我不在。重要的部分是workgh:,它将被 git@github.com 替换,将使用正确的 ssh 密钥。 url 的其余部分(如myWorkLogin/myWorkrepo.git)是您在git@github.com之后使用的普通 GitHub URL
    • 谢谢,在遇到一些麻烦之后,这似乎奏效了。也许这个 git auth 的噩梦终于结束了,我实际上可以写一些代码了!顺便说一句,使用这种方法,我必须注意用这种模式构造远程回购 url 吗?通常我会做git remote add origin &lt;ssh-url-copied-from-newly-constructed-github-repo&gt;。似乎从现在开始,自己的远程 url 需要这个配置感知主机附加到通常的 git ssh-url,对吗?
    • @AlexBollbach 是的,这就是想法:您将经典的git@github.com:myuser/myrepo 添加(或替换为我的回答中的 set-url)mykey:myuser/myrepomykey 是您的一个条目~/.ssh/config 会告诉 ssh 使用 git@github.com,但更重要的是,会告诉 ssh 使用正确的 ssh 密钥。
    猜你喜欢
    • 2010-11-05
    • 2018-09-28
    • 2019-02-07
    • 2016-03-30
    • 2014-07-30
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    相关资源
    最近更新 更多