How to use multiple GitHub account on same device?

Many people use GitHub for both personal and professional projects. However, if you have multiple GitHub accounts, you may be wondering how to use them on the same device.

There are a few different ways to do this. One way is to use different SSH keys…


This content originally appeared on DEV Community and was authored by Shiva Aryal

Many people use GitHub for both personal and professional projects. However, if you have multiple GitHub accounts, you may be wondering how to use them on the same device.

There are a few different ways to do this. One way is to use different SSH keys for each account.

Using SSH keys

To use SSH keys, you will need to generate a new key pair for each account. You can do this by following these steps:

  1. Open a terminal window.
  2. Run the following command to generate a new key pair:
ssh-keygen -t ed25519 -C "my-email@gmail.com"
  1. When prompted, enter a file in which to save the key. Enter "id_personal", "id_work" or leave empty if you want to save as default file name .
  2. When prompted, enter a passphrase for your key pair. Leave empty if you want.
  3. The key pair will be generated in the ~/.ssh directory.
  4. Repeat same process for second account.

Once you have generated a key pair for each account, you will need to add the public key to each account. You can do this by following these steps:

  1. Go to the settings page for your account.
  2. Click on the "SSH and GPG keys" tab.
  3. Click on the "New SSH key" button.
  4. Copy the key by running below command
Macos: pbcopy < ~/.ssh/id_personal.pub
Windows: clip < ~/.ssh/id_personal.pub
  1. In the "Key" field, paste the public key for the account.
  2. Click on the "Add key" button.

Once you have added the public key to each account, you need to create a config file under ~/.ssh/ directory by running the following command:

touch ~/.ssh/config

Now when the config file is created, open with vscode or any code editor and replace below code.

#work account
Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_work

#Personal account
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_personal 

Save the file and run below command

eval "$(ssh-agent -s)"

or

ssh-agent reload

Now use the SSH key to connect to each account. You can do this by following these steps:

  1. Open a terminal window.
  2. Change to the directory where you want to clone the repository.
  3. Run the following command to clone the work repository:
git clone git@github.com-work/repository.git
  1. Run the following command to clone the personal repository:
git clone git@github.com-work/repository.git

Conclusion

Using multiple GitHub accounts on the same device can be a bit tricky, but it is definitely possible. By following the steps in this article, you will be able to use multiple GitHub accounts on the same device without any problems.


This content originally appeared on DEV Community and was authored by Shiva Aryal


Print Share Comment Cite Upload Translate Updates
APA

Shiva Aryal | Sciencx (2023-05-13T16:17:23+00:00) How to use multiple GitHub account on same device?. Retrieved from https://www.scien.cx/2023/05/13/how-to-use-multiple-github-account-on-same-device/

MLA
" » How to use multiple GitHub account on same device?." Shiva Aryal | Sciencx - Saturday May 13, 2023, https://www.scien.cx/2023/05/13/how-to-use-multiple-github-account-on-same-device/
HARVARD
Shiva Aryal | Sciencx Saturday May 13, 2023 » How to use multiple GitHub account on same device?., viewed ,<https://www.scien.cx/2023/05/13/how-to-use-multiple-github-account-on-same-device/>
VANCOUVER
Shiva Aryal | Sciencx - » How to use multiple GitHub account on same device?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/13/how-to-use-multiple-github-account-on-same-device/
CHICAGO
" » How to use multiple GitHub account on same device?." Shiva Aryal | Sciencx - Accessed . https://www.scien.cx/2023/05/13/how-to-use-multiple-github-account-on-same-device/
IEEE
" » How to use multiple GitHub account on same device?." Shiva Aryal | Sciencx [Online]. Available: https://www.scien.cx/2023/05/13/how-to-use-multiple-github-account-on-same-device/. [Accessed: ]
rf:citation
» How to use multiple GitHub account on same device? | Shiva Aryal | Sciencx | https://www.scien.cx/2023/05/13/how-to-use-multiple-github-account-on-same-device/ |

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.