为某个 git 仓库使用指定的私钥进行提交

  • By 王小帅
  • 1 min read
  • Tags: 
  • git

通常我们使用 git 提交代码时,使用的都是一个全局的私钥,但有时候会出现某个项目我需要以另一个身份(私钥)进行提交的情况。此时可以通过在 ~/.ssh/config 中增加一个 ssh 主机配置的方式解决。

Host github-id2
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_id2
    Port 22

然后指定 origin 为配置中的主机:

git remote add origin git@github-id2:path/my-repository.git