Hi there ๐Ÿ‘‹

In this blog we will see how to setup 2 Github accounts on a single computer with 2 SSH keys.

I will be using Linux as my OS

1. Generate SSH Key Pair๐Ÿ”‘

  1. Open Terminal

  2. Generate the key pair using ssh-keygen

$ ssh-keygen -t ed25519 -C "[email protected]"

You will be prompted to enter the filename

> Enter a file in which to save the key (/home/user/.ssh/id_ed25519):[Press enter]

If you press enter without writing the filename the file name will be default i.e (id_ed25519)

You will be prompted another time to enter the passphrase

> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

if you create a passphrase then you have to enter that everytime you use your SSH ๐Ÿ”‘

Step 1 Done โœ…

2. Add Public ๐Ÿ”‘ to your Github Account

Copy your Public Key (the one with .pub extension) and follow the steps in this Link to add it to your Github account

So far we have generated 1 key and added that to Github Account, Repeat ๐Ÿ” steps 1 and 2 again to generate another key and add that to your other Github account

Step 2 Done โœ…

3. Create/Edit ssh config file ๐Ÿ“‚

If you have already created config file in ~/.ssh otherwise create that using touch ~/.ssh/config

Open the config file in your favorite text editor, I will use nano nano ~/.ssh/config

# Personal github account: github_username
Host github.com
   HostName github.com
   IdentityFile ~/.ssh/<key_name>
   IdentitiesOnly yes
   
# Other github account: other_github_username
Host github-other
   HostName github.com
   IdentityFile ~/.ssh/<other_key_name>
   IdentitiesOnly yes

github-other is an alias you can add for your other account, you can change it according to your liking

Step 3 Done โœ…

4. Add SSH private keys to SSH agent โž•

$ ssh-add ~/.ssh/<key_name>
$ ssh-add ~/.ssh/<other_key_name>

Step 4 Done โœ…

5. Test Connection ๐Ÿงช

$ ssh -T [email protected]
$ ssh -T git@github-other
You will get following message in case of successful test:

Hi <github_username>! You've successfully authenticated, but GitHub does not provide shell access.

Step 5 Done โœ…

6. Clone Github Repository

For Personal Github Account (Clone URL is same as the URL you copy from Github Repo)

$ git clone [email protected]:<github_username>/<repo-name>.git
$ cd <repo-name>
$ git config user.email "[email protected]"
$ git config user.name  "username"

For other account, we have to use the alias we assigned in config file

$ git clone git@github-other:<github_username>/<repo-name>.git
$ cd <repo-name>
$ git config user.email "[email protected]"
$ git config user.name  "username"

Step 6 Done โœ