You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置.ssh/config实现同一账号下两个远程仓库的SSH访问

同一GitHub账号多仓库不同SSH密钥的配置方法

问题背景

我有两个项目,对应同一个GitHub账号下的两个仓库。已将对应远程仓库的SSH密钥配置到~/.ssh/config中,但push操作同一时间仅能生效一个仓库的密钥:

IdentityFile ~/.ssh/id_rsa1

尝试在config中添加多个密钥时:

IdentityFile ~/.ssh/id_rsa1
IdentityFile ~/.ssh/id_rsa2

Git抛出错误:

ERROR: Repository not found.
fatal: Could not read from remote repository.

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

若按以下方式配置,仅第一个密钥生效:

Host github.com
IdentityFile ~/.ssh/id_rsa1

Host github.com-repo2
IdentityFile ~/.ssh/id_rsa2

需要找到无需每次编辑config就能让两个密钥同时生效的方法。

错误尝试记录

曾用以下config配置尝试推送:

Host github.com-repo1:me/reponame1
User git
IdentityFile ~/.ssh/id_repo1

Host github.com-repo2:me/reponame2
User git
IdentityFile ~/.ssh/id_repo2

执行命令:

git push -u origin main

仍出现相同的仓库未找到错误。

可行配置步骤

经过测试,以下配置可以正常实现多密钥同时生效:

  1. 为每个项目单独设置远程仓库地址

    • 针对repo1项目,在项目目录下执行:
      git remote set-url origin git@github.com-repo1:accountName/repo1.git
      
    • 针对repo2项目,在项目目录下执行:
      git remote set-url origin git@github.com-repo2:accountName/repo2.git
      
  2. 配置~/.ssh/config文件
    在文件中添加以下内容:

    Host github.com-repo1
      HostName github.com
      IdentityFile ~/.ssh/id_repo1
    
    Host github.com-repo2
      HostName github.com
      IdentityFile ~/.ssh/id_repo2
    
  3. 执行推送操作
    在各自项目目录下执行推送命令即可正常提交:

    git push -u origin main
    

内容的提问来源于stack exchange,提问作者Artur Müller Romanov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 21:50:41