Manage Muplite Github Accounts on Mac
Learn how to setup more than one Github accounts in your Mac with the help of SSH.
Recently I switched from PC to Mac, and I was a bit uncomfortable and exhausted about one thing when it comes to the office works and personal works. I have a personal GitHub account where I experiment with new stuff and do my own projects. Also, I have another GitHub account for my office works. When I was first set up my office account and in my leisure times try to start working on a new project using my personal GitHub account, I faced a problem to clone my repositories and do git operations. It's because I have office GitHub account in my Mac, so it didn't recognize my personal account. So, as everyone will do, I googled it and tried to find a solution. Unfortunately, it was not that much easy, and I had to combine several articles and StackOverflow answers to come up with a working solution. Finally, I was able to do that, and I'll share that process with anyone who interests. So let's dig in!
This article is a practical guide to setting up and manage multiple GitHub accounts in a single machine, so I assume you'll also involve the process.
First of all, you should have two different active GitHub accounts. Let's assume your personal GitHub account is related to
email@example.com, and your office GitHub account is associated with
Next, we have to generate two SSH keys for corresponding GitHub accounts. The following command will create a key for your personal account.
cd ~/.sshssh-keygen -t rsa -b 4096 -C "firstname.lastname@example.org"
This will ask for a name to save the key. Type id_rsa_personal to save it. After it asks for a password, leave it as empty and go ahead.
The following command will create a key for your office account. When it asks for a name to save the key, type id_rsa_office, leave the password as empty like the previous step.
ssh-keygen -t rsa -b 4096 -C "email@example.com"
After successfully running the above command will create four files inside the .ssh directory. You can list those by running this command:
ls -al ~/.ssh
Generated 4 files will look like this,
Now you have two keys. One for your personal account and one for your office account. Please note that the files with .pub extension are files we need to add on GitHub accounts. Next step is for that.
Copy the personal account's public key using the following command. It'll copy the key into clipboard.
pbcopy < ~/.ssh/id_rsa_personal.pub
Go to GitHub and login to your personal account (
firstname.lastname@example.org). Follow these steps.
- Click your avatar on the top right corner and click Settings menu.
- On the left side, under the Personal settings menu, click on SSH and GPG keys.
- On the top right corner, click on New SSH key button.
- Add a meaningful title to identify your key.
- Press cmd + v to paste the copied key.
- 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.
Using your terminal head back to the .ssh folder by typing cd ~/.ssh create a configuration file by using bellow command.
Open and edit the file using nano (or whatever editor you prefer).
Then add the following configurations for two accounts.
# Personal accountHost github.com-personalHostName github.comUser gitIdentityFile ~/.ssh/id_rsa_personal
# Office accountHost github.com-officeHostName github.comUser gitIdentityFile ~/.ssh/id_rsa_office
To save and exit press ctrl+o then press Enter and then press ctrl+x
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 Nameemail = email@example.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 following commands.
Then add following configurations.
[user]email = firstname.lastname@example.org
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.
Now you have to authenticate the keys with GitHub by running following commands.
ssh -T github.com-personalssh -T github.com-office
Now, we're almost done!
Go to your personal GitHub account and either you can create a new repository there or use an existing one. Let's assume your repository name is my-repo. Follow the instructions below and make a clone of your repository on your local machine.
git clone email@example.com:USERNAME/my-repo.git
This will clone your repository, and rest is history. You know what to do, go ahead and create initial commit! Apply same procedure for office account. Please bear in mind, to succeed in this method git assumes all of the office work-related stuff is located in Office folder.
If you have any thoughts, things need to clarify or any suggestions, please add it in the comments section and please don't forget to like and share. Hope you all enjoy :)
Fored the idea of blog from this linkedin post for ease in access and improvement.