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:
- Open a terminal window.
- Run the following command to generate a new key pair:
ssh-keygen -t ed25519 -C "my-email@gmail.com"
- 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 .
- When prompted, enter a passphrase for your key pair. Leave empty if you want.
- The key pair will be generated in the
~/.ssh
directory. - 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:
- Go to the settings page for your account.
- Click on the "SSH and GPG keys" tab.
- Click on the "New SSH key" button.
- Copy the key by running below command
Macos: pbcopy < ~/.ssh/id_personal.pub
Windows: clip < ~/.ssh/id_personal.pub
- In the "Key" field, paste the public key for the account.
- 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:
- Open a terminal window.
- Change to the directory where you want to clone the repository.
- Run the following command to clone the work repository:
git clone git@github.com-work/repository.git
- 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.
Top comments (3)
awesome article
Not tricky at all with most GUI git clients
I agree but terminal feels better