git ssh configuration with multiple accounts

2019-08-04 Last modified on 2021-05-24

Just a quick thing to configure git with ssh when you have multiple accounts.

First, read how to create an SSH key on GitHub and Gitlab.

Let's say we have 2 keys:

  • One for home: ~/.ssh/home and ~/.ssh/home.pub
  • One for work: ~/.ssh/work and ~/.ssh/work.pub

Update the ssh configuration at ~/.ssh/config:

Host github.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/home

Host github-work.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/work

Then, to clone beulogue with (notice the hostname change !):

The home key, use:

git clone git@github.com:SiegfriedEhret/beulogue.git.

The work key, use:

git clone git@github-work.com:SiegfriedEhret/beulogue.git

See you !