# How to work with multiple GitHub Accounts and ssh keys

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 as usual. (see [Generating a new SSH key and adding it to the ssh-agent](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent)).

## Check if the ssh-agent is running.

```shell
eval "$(ssh-agent -s)"
```

## Add your ssh keys to ssh-agent

You can create separate keys and add them manually by:

```shell
$ 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.

```shell
$ ssh-agent -L
```

The output may look as follow:

```plaintext
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.

```plaintext
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:

```shell
$ git clone git@github.com:YourGitHubAccount/my-app.git
```

But if you want to checkout a repo from your work, you need to replace

```plaintext
$ git clone git@work:YourGitHubAccount/my-app.git
```

## How to remove all keys from the ssh-agent

```shell
$ ssh-add -D
All identities removed.
```

Check if the keys were removed successfully:

```shell
$ ssh-add -L
The agent has no identities.
```
