How to Work with GitHub and Multiple Accounts

GitHub provides two ways of connecting to git repositories, mainly 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 now need to have the ability 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 with your email address. 

Be careful that you don’t over-write 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 key in 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.

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

3. Add SSH Key to the Agent

Next, because we saved our key with a unique name, we need to tell SSH about it. Within 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 as 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.

Login 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.

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 

Lets 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+.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Jeffrey Way

GitHub provides two ways of connecting to git repositories, mainly 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 now need to have the ability 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 with your email address. 

Be careful that you don't over-write 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 key in 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.

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

3. Add SSH Key to the Agent

Next, because we saved our key with a unique name, we need to tell SSH about it. Within 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 as 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.

Login 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.

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 

Lets 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+.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Jeffrey Way


Print Share Comment Cite Upload Translate Updates
APA

Jeffrey Way | Sciencx (2014-01-20T02:26:42+00:00) How to Work with GitHub and Multiple Accounts. Retrieved from https://www.scien.cx/2014/01/20/how-to-work-with-github-and-multiple-accounts/

MLA
" » How to Work with GitHub and Multiple Accounts." Jeffrey Way | Sciencx - Monday January 20, 2014, https://www.scien.cx/2014/01/20/how-to-work-with-github-and-multiple-accounts/
HARVARD
Jeffrey Way | Sciencx Monday January 20, 2014 » How to Work with GitHub and Multiple Accounts., viewed ,<https://www.scien.cx/2014/01/20/how-to-work-with-github-and-multiple-accounts/>
VANCOUVER
Jeffrey Way | Sciencx - » How to Work with GitHub and Multiple Accounts. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2014/01/20/how-to-work-with-github-and-multiple-accounts/
CHICAGO
" » How to Work with GitHub and Multiple Accounts." Jeffrey Way | Sciencx - Accessed . https://www.scien.cx/2014/01/20/how-to-work-with-github-and-multiple-accounts/
IEEE
" » How to Work with GitHub and Multiple Accounts." Jeffrey Way | Sciencx [Online]. Available: https://www.scien.cx/2014/01/20/how-to-work-with-github-and-multiple-accounts/. [Accessed: ]
rf:citation
» How to Work with GitHub and Multiple Accounts | Jeffrey Way | Sciencx | https://www.scien.cx/2014/01/20/how-to-work-with-github-and-multiple-accounts/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.