Using Multiple Github Accounts On One Machine

Published on:

Last updated: August 23rd. 2022

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Mondkapje FM

This is a text that is Bold Italic Striketrough Link

  • Bla
  • Bla
  • Bla
  1. Item one
  2. Item two
  3. Item three
  4. Item four
  5. Item five
  6. Item six
  7. Item seven
  8. Item eight
  9. Item nine
  10. Item ten

Quote is this

I just spent the last 3 hours fighting with ssh and git because I used to be able to use multiple multiple GitHub accounts on one machine with different local repo's so I could work on my personal projects as well as work related projects. I made this small tutorial so I would never forget how to do that ever again. Let's get started! The first and most important thing to do is generating a second set of ssh keys. Browse to your ssh folder.

// In your gatsby-config.js plugins: [ { resolve: `gatsby-transformer-remark`, options: { plugins: [ `gatsby-remark-prismjs`, ] } } ]
~ » cd ~/.ssh

Generate the key. The -t option is used to specify the type of key, the -b is the number of bits for the key and the -C is used as a comment to associate with the key.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com" Generating public/private rsa key pair. Enter file in which to save the key (/Users/me/.ssh/id_rsa): id_rsa_work_key Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in id_rsa_work_key. Your public key has been saved in id_rsa_work_key.pub. The key fingerprint is: SHA256:prmt+sL/POed/97AYMO1+OfkDNs2T0JCWXdbXFVLpL8 your_email@example.com The key's randomart image is: +---[RSA 4096]----+ ...(you'll see an image here)

You'll be prompted for a couple of options. You can leave most things as default and just hit Enter.

~/.ssh » ls -la config id_rsa id_rsa.pub id_rsa_work_key id_rsa_work_key.pub known_hosts

Now, you can see that the newly created key has two parts. The file ending in .pub is the public key (duh-doy!) and the file without extension is your private key. Let's take a look at the keys that the ssh-agent already knows about.

~/.ssh » ssh-add -l 2048 SHA256:wYc0jp9VW4pADlLmdxI+k7hW5nXh8K+idsq30+Pn2Bc /Users/me/.ssh/id_rsa (RSA)

This is the (default) private key that was generated by the system or added by you before. Now it's time to add our newly created private key to our ssh-agent with the following command:

~/.ssh » ssh-add id_rsa_work_key Identity added: id_rsa_work_key (your_email@example.com)

After doing this, we can verify if the key was added.

~/.ssh » ssh-add -l 2048 SHA256:wYc0jp9VW4pADlLmdxI+k7hW5nXh8K+idsq30+Pn2Bc /Users/me/.ssh/id_rsa (RSA) 4096 SHA256:hEX0A18xGZ4ZvOar+S6Tmxq48r8gxfkcInC1JTStpuA work.email@work.com (RSA)

Now that our keys are in place, we need a way to tell ssh when to use which key. Luckily for us, ssh has a way of doing just that using a config file.

~/.ssh » nano config

Create a new config file inside your ~/.ssh folder and enter the following information:

Host github.com HostName github.com User git AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_rsa Host github.com-work HostName github.com User git AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_rsa_work_key

Save the file with CTRL+O, hit Enter and quit with CTRL+X. Important to note here is the reference to each private key next to the IdentityFile field. Next to each Host declaration is an alias name. This name will later be used in our git remote definitions but for now we can test our aliases with the following commands:

~/.ssh » git -T github.com

which should output:

Hi <PERSONAL_ACCOUNTNAME>! You've successfully authenticated, but GitHub does not provide shell access.
~/.ssh » git -T github.com-work

which should output:

Hi <WORK_ACCOUNTNAME>! You've successfully authenticated, but GitHub does not provide shell access.

SSH now knows which private key to associate with which alias and we can use this mechanic to our advantage when specifying our git remotes. After creating a new git repository and creating your first commit,

  • create repo's for each account
  • add remotes for each repo (NOTE: after the @ sign is not a domain name but rather an alias!)
  • push to github
» git remote -v origin git@github.com-work:workuser/work-repo.git (fetch) origin git@github.com-work:workuser/work-repo.git (push)
» git remote -v origin git@github.com:personaluser/personal-repo.git (fetch) origin git@github.com:personaluser/personal-repo.git (push)
back