Advertisement
  1. Code
  2. Tools & Tips

How to Work With GitHub and Multiple Accounts

Scroll to top
Read Time: 5 min

GitHub provides two ways of connecting to git repositories, namely SSH and HTTPS. HTTPS requires you to supply an access token every time you push to a repository. SSH allows you to push code without remembering your username and token every time you push code to a GitHub repository.

So you have a personal GitHub account—everything is working perfectly. But then, you get a new job, and you now need to be able to push and pull to multiple accounts. How do you do that? I'll show you how!

1. Create a New SSH Key

We need to generate a unique SSH key for our second GitHub account.

Replace the placeholder with your email address. 

Be careful that you don't overwrite your existing key for your default personal account. Instead, when prompted, save the file as id_rsa_COMPANY. In my case, I've saved the file to ~/.ssh/id_rsa_work.

After you run the command above, you will get a prompt to enter a passphrase. 

A passphrase should be easy to remember but hard for a bot or computer to guess. You could use a favorite line of poetry, for example, or a line from a movie that you like.

You will be prompted to enter the passphrase again.

Once you enter the passphrase again, the key is saved in the default location you specified, and two files are created as shown below.

One is a public key, and the other is a private key. You will need the key to make a connection with your GitHub account. To view the contents of the saved key, issue the following command.

Copy and store the key on a text file on your computer, as we will need to add it to your second GitHub account.


2. Attach the New Key

Next, log in to your second GitHub account, click on the drop-down next to the profile picture at the top right, select Settings, and click on SSH and GPG keys.

SSH and GPG keys settings on GitHubSSH and GPG keys settings on GitHubSSH and GPG keys settings on GitHub

Next, add the key you copied earlier. Feel free to give it any title you wish.

add the SSH key you copied earlieradd the SSH key you copied earlieradd the SSH key you copied earlier

3. Add the SSH Key to the Agent

Next, because we saved our key with a unique name, we need to tell SSH about it. In the Terminal, type: ssh-add ~/.ssh/id_rsa_work. If successful, you'll see a response of Identity Added.

We now have two sets of SSH keys in our machine. They reside in the .ssh directory, and you can view them.


4. Create a Config File

We've done the bulk of the workload, but now we need a way to specify when we wish to push to our personal account and when we should instead push to our company account. To do so, let's create a config file.

If you're not comfortable with Vim, feel free to open the file with any editor of your choice. Paste in the following snippet.

This is the default setup for pushing to our personal GitHub account. Notice that we're able to attach an identity file to the host. Let's add another one for the company account. Directly below the code above, add:

The config file should now look like this:

This time, rather than setting the host to github.com, we've named it github.com-work. The difference is that we're now attaching the new identity file that we created previously: id_rsa_work.

Save the page and exit.


5. Try It Out!

It's time to see if our efforts were successful. Create a test directory, and add a test.txt file.

Initialize git and create the first commit.

Log in to your company account, create a new repository, and give it the name test_repository. Once the new repository is created, GitHub provides you with some next steps, as shown below.

creating new github repositorycreating new github repositorycreating new github repository

Since we already have an existing repository on the command line, we only need to add and push changes. This time, rather than pushing to git@github.com, we're using the custom host we created in the config file: git@github.com-work.

Use

Instead of 

Let's push the changes.

Return to GitHub, and you should now see your repository. Remember:

  • When pushing to your personal account, proceed as you always have.
  • For your company account, make sure that you use git@github.com-COMPANY as the host.

Conclusion

This guide has covered how to work with multiple accounts using SSH. It has also covered how to push code to various GitHub accounts from the same machine.

This post has been updated with contributions from Esther Vaati. Esther is a software developer and writer for Envato Tuts+.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.