Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 17, 2022 03:23 pm

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 [email protected], we're using the custom host we created in the config file: [email protected].


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 [email protected] 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+.



Original Link: https://code.tutsplus.com/tutorials/quick-tip-how-to-work-with-github-and-multiple-accounts--net-22574

Share this article:    Share on Facebook
View Full Article

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code