Dec 12, 2024
#git

Using multiple GitHub account with SSH

You want to use both accounts on same computer (without typing password everytime, when doing git push or pull).


Solution

Use ssh keys and define host aliases in ssh config file (each alias for an account).

How to?

Generating a new SSH key and adding it to the ssh-agent

Reference here.

Navigate to the ~/.ssh folder and run the command below.

bash
ssh-keygen -t ed25519 -C "work@acme.com"
# Generating public/private ed25519 key pair.
# Enter file in which to save the key (/Users/lucle/.ssh/id_ed25519): work
# Enter passphrase (empty for no passphrase): 
# Enter same passphrase again: 

ssh-keygen -t ed25519 -C "personal@acme.com"

Edit/Create ssh config file (~/.ssh/config):

bash
Host github.com-personal
HostName github.com
IdentitiesOnly yes
IdentityFile ~/.ssh/personal

Host github.com-work
HostName github.com
IdentitiesOnly yes
IdentityFile ~/.ssh/work

NOTE: If you use any account frequently, you should use the default hostname (github.com).

Add ssh private keys to your agent:

bash
ssh-add ~/.ssh/personal
ssh-add ~/.ssh/work

Test your connection

bash
ssh-keyscan github.com >> ~/.ssh/known_hosts
ssh -T git@github--personal
ssh -T git@github.com-work