How to use multiple GitHub accounts on one machine
I needed to create a new GitHub account for work, but I didn't want to switch my credentials every time I pushed, cloned, or pulled from my private repositories.
I quickly googled how to achieve this without much hassle, and here it is:
1. Generate the SSH keys for both your work and private emails.
It will ask you to enter a unique file name and passphrase. I liked the file name pattern from the other blog post, so I borrowed (stole) it -
Don't forget to save the passphrase somewhere safe! For example in 1password.
$ cd .ssh/$ ssh-keygen -t rsa -C "firstname.lastname@example.org"$ ssh-keygen -t rsa -C "email@example.com"
I am not sure if you should keep the commands' output, but I did! Just in case!
2. Copy the SSH keys and paste them into the appropriate GitHub account
$ ls$ cat id_rsa_personal.pub | pbcopy$ cat id_rsa_work.pub | pbcopy
Log in to the appropriate GitHub account, go to settings. Click on the
SSH and GPG keys, and paste one of the ssh keys.
3. Setup SSH config
You need to create an SSH config with both hosts configured.
$ vim ~/.ssh/config# replace IdentityFile with your own file name and :wqHost me.github.comHostName github.comUser gitIdentityFile ~/.ssh/id_rsa_personalHost work.github.comHostName github.comUser gitIdentityFile ~/.ssh/id_rsa_work
4. Add the SSH keys to the SSH Agent
$ ssh-add ~/.ssh/id_rsa_personal$ ssh-add ~/.ssh/id_rsa_work
5. Clone the repository to test things out
$ git clone firstname.lastname@example.org:orgName/repoName.git