How to work with multiple github account

You have a personal github account and recently join in a new company. Now you need to maintain your official github account. But you want to use two account at the same time from your computer. In this article I will show you how you get this job done.

You have your personal SSH key for you personal account. Now you need to set up another SSH key for your official account. So open you terminal and run below command. I am in my windows 10 setup and using cmder for terminal.

ssh-keygen -t rsa -C "yournewemail@company.com"

Now be careful. Don’t override your previous ssh with the current. you need to save your carefully. When you been prompted for new name, add new name like id_rsa_COMPANY . I use my company name and save it.

After successfully generated the SSH key, you can now check your new SSH key from the .ssh directory.

Now you can add your new key to your new github account. Simply open the .ssh directory and copy the code from id_rsa_company.pub and paste in your new github account.

Now you have already added the SSH key to your github account.

We have added a new key with a unique name. We need to tell SSH about it. Open your terminal and paste below command.

ssh-add ~/.ssh/id_rsa_company

If successful you will get a message that a new like a new identity added.

If you are in windows like me and using cmder you might not get the success message at the first time. You need to remove a default environment variable from your path.

%SYSTEMROOT%\System32\OpenSSH\

Remove the above from your path variable.

Now you need to start your ssh agent. Run below command from you terminal.

start-ssh-agent

Then run the add command again.

ssh-add ~/.ssh/id_rsa_company

Now you will successfully add you new SSH key.

Add a config file

In you .ssh directory you need to add a config file .

touch config

Now open that config file with any code editor and paste below code inside that config file.

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

Host github-company
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_company

You can skip the first one. Because its default.

You might notice that, I add github-company as a second host name. You can you add yours name like you SSH key.

Push you code

You can create a new repo or work with an existing repo.

For this tutorial I am using existing repo. Add a repo origin in your directory like below.

git remote add origin git@github-company:companyname/testing.git

You need to add that config host name after git@ .

Note that this time we are using git@github-company instead of git@github.com .

If everything goes right, you can push your code to you new account.

You can finally push your code to github repo as like previous way.

git push origin master

Clone a repo

Now we will discuss about cloning a official repo.

It so easy. Let assume you have a office repo like below.

git@github.com:companyname/workingsite.git

Now you need to replace .com with your host name like below.

git@github-company:companyname/testing.git

Replace this code with your host name as we set in the config file.

Now you can clone your official repo like before.

Let me know, if you face any problem. Thanks for reading.

Published
Categorized as Dev, Git