How to Add Multiple Github Accounts on One Computer (Linux)
Tags: github, linux, ssh, productivity
Published: 2020-10-14 19:52:25

Normally, I like to separate work and personal projects by managing different accounts. However, occasionally we might bump into cases where we only have one computer but have to use it for both work and personal matters. Thus, we might need to set up two or more github accounts on one computer. Here is a simple guide on how to do that. 

Let's assume that we need to set up two accounts:

- Annie, github username: annie (annie@annie.com) - default, personal, global
- AnnieWork, github username: anniework (annie@anniework.com) - work only

# 1. Generating the SSH keys

First let's check if we already have it on our computer by 
```
ls -al ~/.ssh
```
If we see the id_rsa available, we could use this as our default key for Annie account. 

In case we don't have anything, we can generate a default key by typing (-t means type. The possible values are “dsa”, “ecdsa”, “ecdsa-sk”, “ed25519”, “ed25519-sk”, or “rsa”. This flag may also be used to specify the desired signature type when signing certificates using an RSA CA key. The available RSA signature variants are “ssh-rsa” (SHA1 signatures, not recom‐mended), “rsa-sha2-256”, and “rsa-sha2-512” (the default).)

```
ssh-keygen -t rsa
```

When asked for location, you just press enter to use the default setting. The default private key and public key will be created at ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub. 

For our work account AnnieWork, we need to generate another different SSH key. We save it as ~/.ssh/id_rsa_anniework.
```
ssh-keygen -t rsa -C "annie@anniework.com" -f "id_rsa_anniework"
```

Now we should see two different keys created:
```
~/.ssh/id_rsa
~/.ssh/id_rsa_anniework
```
# 2. Adding keys to respective Github accounts

To add ssh keys to github, so we don't need to constantly type our username and password when we want to push something to github. First we need to copy the public key. You can simply go to file folder, open it with an editor and copy, or run this command to do that. 
```
pbcopy < ~/.ssh/id_rsa.pub
```
Now login to your github account (the Annie account, since we're using the default key here). 

Settings > SSH and GPG keys > New SSH Key > Enter a name for your computer, this name should be recognizable so you know which computer it is related to > paste the key you just copied, which looks like a bunch of gibberish letter and numbers > Add key > DONE! 

Now for the work account, you repeat the above steps, except you need to copy the key from id_rsa_anniework.pub (work account) instead of id_rsa.pub (default account) and you need to add the key to your Anniework github account. 

# 3. Register the new SSH keys with ssh-agent 

To use the keys, we need to register them with the ssh-agent on our computer. 

```
eval "$(ssh-agent -s)"
Agent pid 132819 //this is what you will see
```

That gets your ssh-agent running, then you add the keys:

```
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_anniework
```

# 4. Create SSH config file

Usually we don't have the config file, so now we can create one to add SSH configuration rules for different hosts. 

```
cd ~/.ssh/
touch config      //create config file
nano config       //use nano to edit the config file 
```

Now in your ~/.ssh/config file, paste the following:

```
# Default, personal, global - Annie
Host github.com
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa
   
# Work - AnnieWork
Host github.com-anniework    
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_anniework
```

"github.com-anniework" is a notation to differentiate the work github account. Of course you can also set it up as "anniework.github.com", just be consistent when clone the repository. 

Another approach is not to create config rules but manually ensure the ssh-agent to attach only one key at a time with any git operation. To do that you can remove all the keys, then add the key you plan to use. 

```
ssh-add -D       //remove all ssh entries in the ssh-agent
ssh-add ~/.ssh/id_rsa   //now we add the default key
```

Repeat the step when you need to use the work account, just remember to replace the "id_rsa" with "id_rsa_anniework". I personally found this approach a bit troublesome, and prefer the config file approach. But choose whatever suit you the best. 

# 5. Set up the git config user.name and user.email

For the default and global one, you can do

```
git config --global user.name "Annie"
git config --global user.email "annie@annie.com"
git config -l
user.name=Annie
user.email=annie@annie.com
```

In this way, when you push code it will be the default setting. 

But when you need to work in the work repository, make sure you set it up locally 

```
git config user.name "AnnieWork"
git config user.email "annie@anniework.com"
```

# 6. How to clone the repositories

I assume you already know how to clone the repository normally, so I will only cover the work one. When you copy the ssh key you need to modify it like this:

```
git clone  git@github.com-anniework/repo_name.git
```

You remember earlier in our ssh config file we set the work account host as "github.com-anniework" right? We need to use this exact name after "git@" when you do git clone. 

Double check if we have done it right 

```
git remote -v
```

Or if we need to update it we do it like:

```
git remote set-url origin git@github.com-anniework:anniework/repo_name.git
```

Note the in the "git@github.com-anniework:anniework/repo_name.git", after "git@", it's the host name we set in config file, here "github.com-anniework", then after ":", it's our github username, "anniework", it's advised that you keep the name consistent so you don't get confused. 


If we have a local repo that we want to push to github, then we first do git init, then 

```
git remote add origin git@github.com-anniework:anniework/repo_name.git
```

Then proceed like you usually do and you will find that your work account is adding git commit to your work repository.  

It might seem lengthy and slow to set at first time, but once you have it set it up you don't need to care so much! Your git work flow should just work smoothly. :)
![slow happy](https://media.giphy.com/media/3NtY188QaxDdC/giphy.gif)