The Daily TIL

February 19, 2019 by AndyAndygitgithubssh

You can use multiple github SSH keys in the same environment

I have 2 github accounts. My work account and my personal account. In the past, i’ve had trouble working with both accounts on one laptop. This is mainly because my personal account has an old SSH-key created on a different laptop.

I don’t want to add both SSH keys to both accounts on github, so on my work laptop, i used to use HTTPS git with my personal account and SSH git with my work account. This got super annoying so i figured out how to use multiple git-ssh keys on one environment.

Basically, you need both ssh keys, and a modified ssh config.

Example

SSH config

assuming i have 2 ssh keys, one for work, and one for personal…

  • Personal key: id_personal/id_personal.pub
  • Work key: id_work/id_work.pub

… this is my ~/.ssh/config

Host github-work
 HostName github.com
 AddKeysToAgent yes
 UseKeychain yes
 User git
 IdentityFile ~/.ssh/id_work

Host github-personal
 HostName github.com
 AddKeysToAgent yes
 UseKeychain yes
 User git
 IdentityFile ~/.ssh/id_personal

Cloning repos

# cloning using your personal account

$ git clone git@github-personal:personalandy/project.git 
$ cd project
$ git config user.name personalandy
$ git config user.email personalandy@gmail.com

… or you can use ssh:// prototol, which skips the git@ user…

# cloning using your work account

$ git clone ssh://github-work/workandy/project.git 
$ cd project
$ git config user.name workandy
$ git config user.email workandy@gmail.com

References