本地多个Github账户的配置

2025年10月13日

首先,想要使用多个git账户,必须要创建多个不同的SSH公钥和私钥对,可以参考Github文档进行创建,并在Github中配置SSH。

以创建公司用账号duanjian-fe和私人账号username为例:

编辑/新增本地ssh配置文件:~/.ssh/config

添加如下配置:

复制代码
Host duanjian-fe.github.com
  Port 443
  HostName ssh.github.com
  IdentityFile ~/.ssh/id_rsa_poper
  User git
Host username.github.com
  Port 443
  HostName ssh.github.com
  IdentityFile ~/.ssh/id_rsa_username
  User git

注意:此处使用443端口和ssh.github.com是因为开启代理增强模式后,许多节点会关闭SSH默认的22端口,出现Git连接失败的情况。
配置完成后,可以通过以下命令测试:

bash 复制代码
ssh -T duanjian-fe.github.com
ssh -T username.github.com

出现Hi duanjian-fe! You've successfully authenticated字样说明连接成功。

注意: github 默认的 Host 为github.com,我们相当于修改为了duanjian-fe.github.comusername.github.com。所以在clone时要注意修改ssh链接中的github.com。对于已经clone过的仓库,可以在.git/config文件中手动修改remote origin。(格式类似git clone git@duanjian-fe.github.com:duanjian-fe/demo.git

可以通过使用CLI程序来简化多SSH Git用户的操作 git-ssh-switch

相关文章