Managing multiple GitHub SSH logins on a single machine
Tags: github, git, ssh
Published: 2024-04-02 19:59:04

Do you have more than one account for GitHub, i.e. one for professional work dev and another for personal side projects?

If you're using SSH to clone repositories this might help...

### Setting up your SSH keys

I won't copy the existing [GitHub docs](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent) for generating keys

___
Once you've created the keys you should add them to your GitHub accounts, again [their article](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account) would be better than mine...
___
Now that you've got at least 2 SSH keys you need to add some config to `~/.ssh/config`, if that file doesn't exist yet you can create it.

In that file, you need to add an entry like this for each GitHub account
```sh
Host github.com
    Hostname github.com
    User git
    AddKeysToAgent yes
    IdentitiesOnly yes
    IdentityFile ~/.ssh/id_fizz
```
For each one you need to change the `Host` and the `IdentityFile`. i.e. if you had a personal and a work account you could set it up like this:
```sh
Host github.com
    Hostname github.com
    User git
    AddKeysToAgent yes
    IdentitiesOnly yes
    IdentityFile ~/.ssh/id_fizz

Host work-github.com
    Hostname github.com
    User git
    AddKeysToAgent yes
    IdentitiesOnly yes
    IdentityFile ~/.ssh/id_buzz
```
Now you've done that you should be able to be able to test you're connections like so:
```
$ ssh -T git@github.com
Hi <Personal GitHub Username>! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T git@work-github.com
Hi <Work GitHub Username>! You've successfully authenticated, but GitHub does not provide shell access.
```
Now you've done this you can clone repos like this:
```
### personal ###
$ git clone git@github.com:fizz/buzz.git


### work ###
$ git clone git@work-github.com:fizz/buzz.git
```

### Updating your Git config to manage this for you

You might want to set up you're gitconfig files to handle this for you. There are a few ways of doing this but I do it like this.
In my root .gitconfig file, I have this configured:
```sh
[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig-work

[includeIf "gitdir:~/personal/"]
    path = ~/personal/.gitconfig-personal
```

And then in my `~/personal/` and `~/work/` directory, I have gitconfig that is specific to each user. For example:

**.gitconfig-personal**
```
[user]
  email = buzz.fizz@personal.co
  name = mg
  username = buzz
```

**.gitconfig-work**
```
[user]
  email = fizz.buzz@work.co
  name = mg
  username = fizz

[url "git@work-github.com:"]
  insteadOf = "git@github.com:"

```
In the `work` based config I have a [URL remapping](https://git-scm.com/docs/git-config#Documentation/git-config.txt-urlltbasegtinsteadOf). 
So in you're work directory you no longer need to do `$ git clone git@work-github.com:fizz/buzz.git` you can do `$ git clone git@github.com:fizz/buzz.git` as you would when you normally clone something from GitHub.

(Thanks to @ccoveille for making me aware of this!)

___
I recently found out how to do it and thought I'd share, there might be better ways of doing it. Please let me know if there is!