If you need to work with different GitHub accounts, e.g., when you have personal and work accounts, you may want to create separate ssh keys for each account. You can do this by creating multiple keys in the usual way. (see Generating a new SSH key and adding it to the ssh-agent).
Check if the ssh-agent is running.
eval "$(ssh-agent -s)"
Add your ssh keys to ssh-agent
You can create separate keys and add them manually by:
$ ssh-add --apple-use-keychain ~/.ssh/id_work
$ ssh-add --apple-use-keychain ~/.ssh/id_home
Check if the keys were added to the ssh-agent successfully.
$ ssh-agent -L
The output may look as follow:
ssh-ed25519 BBBBD3NzaC2lZDI1MTE7ACBAIDbADwSLXnei9x8AyNJ9W1AcXAgpz7ZDJOpO4WWnUnRK me@work.com
ssh-ed25519 BBBBD3NzaC2lZDI1NTE6AAAAIDbADwSLXmei9x8AyNJ9W1AcXAgpz7ZDJOpO4WWnUnRK me@home.com
How to automatically add both keys when starting up
Open and create `~/.ssh/config` and add both keys to the file.
Host github.com
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_home
User git
Host work
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_work
User git
In this configuration, checking out a repo for your home use follows the standard format:
$ git clone git@github.com:YourGitHubAccount/my-app.git
But if you want to checkout a repo from your work, you need to replace
$ git clone git@work:YourGitHubAccount/my-app.git
How to remove all keys from the ssh-agent
$ ssh-add -D
All identities removed.
Check if the keys were removed successfully:
$ ssh-add -L
The agent has no identities.
Comments