How to use multiple GitHub accounts from a single machine

Photo by Angela M. from Unsplash

If you are reading this article, it means you know the pain of switching between your work and personal GitHub accounts. In this article we will learn in few easy steps to work with multiple GitHub accounts from a single machine.

We will use SSH protocol to connect to GitHub accounts. I assume that you are already working with a GitHub account, let’s call it as “Work account”. And now you want to simultaneously use your second GitHub account, let’s call it as “Personal account”. Without further ado, let’s start with the first step.

1. Generate new SSH key

First generate a new SSH key for your personal GitHub account. Use following command by replacing your_email@example.com with your actual personal GitHub account email. This creates a new SSH key, using the provided email as a label.

ssh-keygen -t rsa -C "your_email@example.com"

Then you will be prompted to “Enter a file path in which to save the key”. Copy and paste the path to your .ssh folder followed by file name id_rsa_mypersonalaccount.

/Users/<YOUR_USERNAME>/.ssh/id_rsa_mypersonalaccount
SSH key generation for your personal account

Next you will be prompted to enter a secure passphrase and after this step your new ssh key will be available in your .ssh folder.

2. Add SSH key to GitHub account

Now add this new ssh key generated in the last step to your personal GitHub account.

Copy the SSH public key to the clipboard

On MAC:

pbcopy < ~/.ssh/id_rsa_mypersonalaccount.pub

On Windows:

clip < ~/.ssh/id_rsa_mypersonalaccount.pub

On Linux:

cat ~/.ssh/id_rsa_mypersonalaccount.pub

Add copied ssh key to your Github account

  1. Visit your GitHub account on any browser.
  2. In the upper-right corner of your GitHub account page, click your profile photo, then click Settings.
  3. In the user settings sidebar, click SSH and GPG keys.
  4. Click New SSH key or Add SSH key.
  5. Paste your key into the “Key” field. You can provide any meaningful name to the key in “Title” field.
  6. Click Add SSH key and then the key should be successfully added to your GitHub account.
Steps to add SSH key to your GitHub account

3. Add SSH config file

Next we will add the SSH configuration rules for different hosts, stating which identity file to use for which domain.

The SSH config file should be available at~/.ssh/ path. Edit the config file if it already exists or else create a new one.

Add below configuration to this ~/.ssh/config file.

https://medium.com/media/74f90aef82cc41497d23be4026c9fa69/href

Account 1 (work)” is the default GitHub account. Your default Github account will work as it is, as it was working earlier.

Account 2 (personal)” with Host github.com-mypersonalaccount” is for your personal GitHub account. You will use it when you want to clone a repository or when you set the remote origin for a local repository.

4. Setting remote URL for the local repositories

When working with your default GitHub account, the commands will remain same. For example, you can clone a repository from your default GitHub account using following command.

git clone git@github.com:work_account_name/repo_name.git

When you want to work with a repository from your second GitHub account (Personal account), use github.com-mypersonalaccount host to reach the repository.

To clone repository from your personal GitHub account:

git clone git@github.com-mypersonalaccount:mypersonalaccount/repo_name.git

You can verify new remote URL withgit remote -v

5. Setting GitHub config username and email

GitHub identifies the author of any commit from the email id attached with the commit description. We have to make sure that we are using correct username and email when dealing with local GitHub repository.

For your personal GitHub account, set username and email for your local repository to your correct personal GitHub account.

git config user.name "your_personal_account_username"
git config user.email "your_email@example.com"

To verify that correct configs are set, use git config –list

And now you are all set to use your multiple GitHub accounts from a single machine.

“Life is a lot simpler without worrying about switching GitHub accounts every time you want to work on your personal GitHub projects.”


How to use multiple GitHub accounts from a single machine was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Manju

Photo by Angela M. from Unsplash

If you are reading this article, it means you know the pain of switching between your work and personal GitHub accounts. In this article we will learn in few easy steps to work with multiple GitHub accounts from a single machine.

We will use SSH protocol to connect to GitHub accounts. I assume that you are already working with a GitHub account, let's call it as “Work account”. And now you want to simultaneously use your second GitHub account, let's call it as “Personal account”. Without further ado, let's start with the first step.

1. Generate new SSH key

First generate a new SSH key for your personal GitHub account. Use following command by replacing your_email@example.com with your actual personal GitHub account email. This creates a new SSH key, using the provided email as a label.

ssh-keygen -t rsa -C "your_email@example.com"

Then you will be prompted to “Enter a file path in which to save the key”. Copy and paste the path to your .ssh folder followed by file name id_rsa_mypersonalaccount.

/Users/<YOUR_USERNAME>/.ssh/id_rsa_mypersonalaccount
SSH key generation for your personal account

Next you will be prompted to enter a secure passphrase and after this step your new ssh key will be available in your .ssh folder.

2. Add SSH key to GitHub account

Now add this new ssh key generated in the last step to your personal GitHub account.

Copy the SSH public key to the clipboard

On MAC:

pbcopy < ~/.ssh/id_rsa_mypersonalaccount.pub

On Windows:

clip < ~/.ssh/id_rsa_mypersonalaccount.pub

On Linux:

cat ~/.ssh/id_rsa_mypersonalaccount.pub

Add copied ssh key to your Github account

  1. Visit your GitHub account on any browser.
  2. In the upper-right corner of your GitHub account page, click your profile photo, then click Settings.
  3. In the user settings sidebar, click SSH and GPG keys.
  4. Click New SSH key or Add SSH key.
  5. Paste your key into the “Key” field. You can provide any meaningful name to the key in “Title” field.
  6. Click Add SSH key and then the key should be successfully added to your GitHub account.
Steps to add SSH key to your GitHub account

3. Add SSH config file

Next we will add the SSH configuration rules for different hosts, stating which identity file to use for which domain.

The SSH config file should be available at~/.ssh/ path. Edit the config file if it already exists or else create a new one.

Add below configuration to this ~/.ssh/config file.

Account 1 (work)” is the default GitHub account. Your default Github account will work as it is, as it was working earlier.

Account 2 (personal)” with Host github.com-mypersonalaccount” is for your personal GitHub account. You will use it when you want to clone a repository or when you set the remote origin for a local repository.

4. Setting remote URL for the local repositories

When working with your default GitHub account, the commands will remain same. For example, you can clone a repository from your default GitHub account using following command.

git clone git@github.com:work_account_name/repo_name.git

When you want to work with a repository from your second GitHub account (Personal account), use github.com-mypersonalaccount host to reach the repository.

To clone repository from your personal GitHub account:

git clone git@github.com-mypersonalaccount:mypersonalaccount/repo_name.git

You can verify new remote URL withgit remote -v

5. Setting GitHub config username and email

GitHub identifies the author of any commit from the email id attached with the commit description. We have to make sure that we are using correct username and email when dealing with local GitHub repository.

For your personal GitHub account, set username and email for your local repository to your correct personal GitHub account.

git config user.name "your_personal_account_username"
git config user.email "your_email@example.com"

To verify that correct configs are set, use git config --list

And now you are all set to use your multiple GitHub accounts from a single machine.

“Life is a lot simpler without worrying about switching GitHub accounts every time you want to work on your personal GitHub projects.”


How to use multiple GitHub accounts from a single machine was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Manju


Print Share Comment Cite Upload Translate Updates
APA

Manju | Sciencx (2021-12-29T23:01:35+00:00) How to use multiple GitHub accounts from a single machine. Retrieved from https://www.scien.cx/2021/12/29/how-to-use-multiple-github-accounts-from-a-single-machine/

MLA
" » How to use multiple GitHub accounts from a single machine." Manju | Sciencx - Wednesday December 29, 2021, https://www.scien.cx/2021/12/29/how-to-use-multiple-github-accounts-from-a-single-machine/
HARVARD
Manju | Sciencx Wednesday December 29, 2021 » How to use multiple GitHub accounts from a single machine., viewed ,<https://www.scien.cx/2021/12/29/how-to-use-multiple-github-accounts-from-a-single-machine/>
VANCOUVER
Manju | Sciencx - » How to use multiple GitHub accounts from a single machine. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/29/how-to-use-multiple-github-accounts-from-a-single-machine/
CHICAGO
" » How to use multiple GitHub accounts from a single machine." Manju | Sciencx - Accessed . https://www.scien.cx/2021/12/29/how-to-use-multiple-github-accounts-from-a-single-machine/
IEEE
" » How to use multiple GitHub accounts from a single machine." Manju | Sciencx [Online]. Available: https://www.scien.cx/2021/12/29/how-to-use-multiple-github-accounts-from-a-single-machine/. [Accessed: ]
rf:citation
» How to use multiple GitHub accounts from a single machine | Manju | Sciencx | https://www.scien.cx/2021/12/29/how-to-use-multiple-github-accounts-from-a-single-machine/ |

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.