DEV Community

Cover image for How to use multiple GitHub account on same device?
Shiva Aryal
Shiva Aryal

Posted on

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 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"
Enter fullscreen mode Exit fullscreen mode
  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
Enter fullscreen mode Exit fullscreen mode
  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
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. Run the following command to clone the personal repository:
git clone git@github.com-work/repository.git
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (3)

Collapse
 
kirankumal profile image
kirankumal

awesome article

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Not tricky at all with most GUI git clients

Collapse
 
sajag_dhungana_2e5fcfc0e6 profile image
Sajag Dhungana

I agree but terminal feels better