Managing Multiple Github Accounts
Tags: github, webdev, macos
Published: 2022-09-23 06:14:17

Let’s see how many GitHub accounts can be managed from a single computer. In essence, it is a matter of balancing your git and ssh configurations, which is not as difficult as it may appear.


> _This guide is intended for Unix users._


**Set up SSH Keys**
On your local workstation, you can create a new SSH key. After generating the key, you can add it to your GitHub account to allow SSH authentication for Git activities.

> Note: On March 15, 2022, GitHub increased security by removing obsolete, unsafe key types.
> DSA keys (ssh-dss) are no longer supported as of that date. You cannot add additional DSA keys to your own GitHub account.
> RSA keys (ssh-rsa) with a valid_after date prior to November 2, 2021 may continue to employ any signature algorithm. After that date, RSA keys must be produced using the SHA-2 signature technique. Some older clients may require an upgrade to utilise SHA-2 signatures.

Assume that your two Github accounts are named githubPersonal and githubWork.

Create two SSH keys and store them in different files:

```
$ cd ~/.ssh
$ ssh-keygen -t rsa -C "personal@email.com"
# save it as id_rsa_personal when prompted
$ ssh-keygen -t rsa -C "work@email.com"
# save it as id_rsa_work when prompted
```

With the above commands, the following files are created:

```
id_rsa_personal
id_rsa_personal.pub
id_rsa_work
id_rsa_work.pub
```

**Adding a new SSH key to your GitHub Personal account**
Copy the SSH public key to your clipboard.

> If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.

```
$ pbcopy < ~/.ssh/id_rsa_personal.pub
# Copies the contents of the id_rsa_personal.pub file to your clipboard
```

> Tip: If `pbcopy` isn't working, you can locate the hidden `.ssh` folder, open the file in your favorite text editor, and copy it to your clipboard.

Add the key to your account:

- Go to your Account Settings
- Click “SSH Keys” then “Add SSH key”
- Paste your key into the “Key” field and add a relevant title
- Click “Add key” then enter your GitHub password to confirm
Repeat the process for your GitHub work account.

**Adding your SSH key to the ssh-agent**
If you’re using macOS Sierra 10.12.2 or later, you will need to modify your `~/.ssh/config` file to automatically load keys into the ssh-agent and store passphrases in your keychain.

- First, check to see if your `~/.ssh/config` file exists in the default location.

```
$ open ~/.ssh/config
> The file /Users/you/.ssh/config does not exist.
```

- If the file doesn’t exist, create the file.
`$ touch ~/.ssh/config`

- Open your `~/.ssh/config` file, then modify the file to contain the following lines. If the name or path of your SSH key file is different from the example code, change the filename or path to match your setup.

```
# githubPersonal
Host personal
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa_personal
# githubWork
Host work
  HostName github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa_work
```

**Update stored identities**
Clear currently stored identities:

`$ ssh-add -D`

Add new keys:

```
$ ssh-add -K ~/.ssh/id_rsa_personal
$ ssh-add -K ~/.ssh/id_rsa_work
```

Test to make sure new keys are stored:

`$ ssh-add -l`
Test to make sure Github recognizes the keys:

```
$ ssh -T personal
Hi githubPersonal! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T work
Hi githubWork! You've successfully authenticated, but GitHub does not provide shell access.
```

**Make default account global**
```
git config --global user.name "personal"
git config --global user.email "personal@email.com"
```

**Test PUSH**
Make a new repository called “test-personal” in your personal account on Github.

Back on your local machine, create a test directory:

```
$ cd ~/documents
$ mkdir test-personal
$ cd test-personal
```

Add a blank `readme.md` file to Github and push:

```
$ touch readme.md
$ git init
$ git add .
$ git commit -am "first commit"
$ git remote add origin git@personal:githubPersonal/test-personal.git
$ git push origin master
```

> Notice how we’re using the custom account, `git@personal`, instead of `git@github.com`.

Repeat the process for your githubWork account.

**Test PULL**
Add some text to the `readme.md` file in your personal account on Github.

Now Pull and merge the changes by running the following command within the test-personal directory:

```
$ git pull origin master
```
Again, repeat this for your githubWork account.

**Clone a repository**
```
$ git clone git@personal:githubPersonal/test-personal.git
# For personal repository
$ git clone git@work:githubWork/test-personal.git
# For Work repository
```

Make sure you add local config to the project folder if you’re using a non-default(githubWork) account.

```
$ git clone git@work:githubWork/test-personal.git
$ git config user.name "work"
$ git config user.email "work@email.com"
```