Follow

Follow
Setup multiple Github accounts on mac. ๐Ÿ’ป

Setup multiple Github accounts on mac. ๐Ÿ’ป

Rayan Abid's photo
Rayan Abid
ยทJul 19, 2022ยท

5 min read

Play this article

Table of contents

  • Introduction

Introduction

So, I was once a happy boy. I just had one Github account, That I could use to do all that git stuff, but then I got hired and was required to use Github with company email. That shouldn't be a big deal but who likes git profiles with no commits on them. So to continue that grind I had to find a way to use multiple accounts on the same machine because last I checked I didn't have money to buy two separate machines.

So I looked around and finally found a solution and thought of sharing it with you guys. So let's start hacking.

Before getting started

Now that you are here I assume you already have the two emails ready. So I'll be referring to these emails as

- Personal

- Work

Creating the SSH keys for your accounts.

First, we will have to generate the key for the Git accounts. So open your terminal and enter the following command.

cd ~/.ssh

If for some reason you get an error saying that this path does not exist, Then don't worry you will have to create this directory using the command.

mkdir ~/.ssh

This will create this dir. After you've done that run this command.

ssh-keygen -t rsa -b 4096 -C "personal@email.com"

Now you will be asked to name this key. You can type id_rsa_personal and save that. You might be asked to enter a password just leave that empty and press enter.

Now do the same for the work email

ssh-keygen -t rsa -b 4096 -C "work@email.com"

Now name the key id_rsa_office and leave the password empty.

After successfully running the above commands will create four files inside the .ssh directory. You can list those by running this command:

After you're done running these commands it will create 4 files inside the .ssh directory. If you wanna see it you can run the following command.

ls -al ~/.ssh

The generated files should look like this,

id_rsa_personal
id_rsa_personal.pub
id_rsa_office
id_rsa_office.pub

Now we have the keys required for Github.

Adding SSH keys to GitHub accounts

Ok, so the process that I'll mention will be the same for the other accounts as well. So you can just follow the same steps for the other accounts as well.

To copy the key you can run this command.

pbcopy < ~/.ssh/id_rsa_personal.pub

If you want to get copy the work email you can use this command.

pbcopy < ~/.ssh/id_rsa_office.pub

Now go to Github log in using your personal account or the other account. Now you just have to follow these 6 steps.

  1. Click your avatar on the top right corner and click the Settings menu.

Screen Shot 2022-07-19 at 12.29.48 AM.png

  1. On the left side, under the Personal settings menu, click on SSH and GPG keys.

Screen Shot 2022-07-19 at 12.39.16 AM.png

  1. On the top right corner, click on New SSH key button.

Screen Shot 2022-07-19 at 12.36.28 AM.png

  1. Add a meaningful title to identify your key.

  2. Press cmd + v to paste the copied key.

  3. Click on Add SSH key to add your key and enter your password if it is asked.

Go through the same steps to add your office SSH key.

Creating configuration files to add two keys

Now head back to your terminal enter cd ~/.ssh create a configuration file by using bellow command.

touch config

This will create a file named config.

nano config

This will open the file using the nano editor

Then add the following configurations for two accounts.

# Personal account
Host github.com-personal
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_personal

# Office account
Host github.com-office
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_office

To save and exit press ctrl+o then press Enter and then press ctrl+x

Creating git configuration files

Using two git configuration files for our personal and office accounts will reduce the stress of managing those accounts by automating the manual process. We'll create a one git configuration file globally for our personal account and the other one in our Office works folder.

Using your terminal navigate to the root folder to create a global git configuration file for your personal account.

cd ~
nano .gitconfig

Then add following configurations.

[user]
    name = Your Name
    email = personal@email.com

[includeIf "gitdir:~/office/"]

    path = ~/office/.gitconfig

Please note that to work the office configuration above, git assumes you have all the office work-related stuff located in a folder called Office. So if it's not the case for you, please feel free to change above config according to your folder structure

Then, create an office work-related git configuration file. To do that, navigate to your Office folder and type the following commands.

nano .gitconfig

Then add following configurations.

[user]

    email = office@email.com

Save key identities in local machine

First, we have to remove any existing identities by running the following command.

cd ~
ssh-add -D

After delete, check if the newly added keys were saved successfully by running the following command.

ssh-add -l

Now you have to authenticate the keys with GitHub by running following commands.

ssh -T github.com-personal
ssh -T github.com-office

Now you can use your multiple accounts on your Mac.

Now if you want to use your personal email you will have to pass -personal after github.com as shown below.

git@github.com-persoanl:USERNAME/REPO_NAME.git

If you face any issues or have any questions please do ask in the comments below.

This was inspired by Nadun Malinda blog on the same topic.

Follow me on Github and Youtube

Bye for now.

ย 
Share this