How to configure and use multiple GitHub accounts on the same machine

As software engineers, we usually create a single account in Github. We create different repositories in it and maintain our source code or any other kind of files in there. But situations may arise when you want to have multiple Github accounts and have a seamless operating strategy in your system to work with those. The reasons can be many e.g. you may want to have separate accounts for your personal projects and open-source projects.

Through this blog, you will get to know the exact step-by-step processes of setting up multiple Github accounts on the same machine and how to use them smoothly.

First, you need to check whether you have “.ssh” or not in your machine. If you want to know more about SSH (Secure Shell) you can check here

Open terminal and run the below command:

ssh -V

It will show the version of SSH present in your system. If it’s not there you will get a response indicating that the command is not found.

Move inside the “~/.ssh” directory and run the below two commands one by one:

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

The emails (“[email protected]” and [email protected]“) should be the actual ones used for the respective Github accounts. The “ssh-keygen” command is used to generate SSH key pairs for authentication. The “-t ed25519” option specifies the type of SSH key to create. Here, it’s an “ed25519” key. Ed25519 is an elliptic curve key pair algorithm that is considered secure and efficient. Using the above commands, you are essentially creating two pairs of SSH public/private key combinations for the accounts – one for your work and one for personal projects. After the commands are run, the “.ssh” directory and file structure inside it should look something like below:

~/.ssh/id_ed25519_personal       
~/.ssh/id_ed25519_personal.pub 
~/.ssh/id_ed25519_work
~/.ssh/id_ed25519_work.pub

After this, run the below command:

eval "$(ssh-agent -s)"

This command will start the SSH agent on your system.

Next, inside the “~/.ssh” directory, you need to have the “config” file. If it’s not there, create one using the command:

touch config

You need to then fill up this config file. If you’re wondering what’s the use of this SSH config file – it is a configuration file for the OpenSSH client using which you can customise SSH client behaviour and settings on a per-host basis (or globally). So we need to add some info inside it for our purpose. Open and fill up the config file like below:

#GitHub personal
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal

#Github work
Host github.com-work      
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work        

Host *
   AddKeysToAgent yes                       
   IdentitiesOnly yes

Note the private keys you will be using here – one is dedicated to your personal account and the other one to your work account. Notice the host names too. For your personal one, you are using “github.com” but for the work one it is mentioned “github.com-work“. You can use some other custom hostname too here e.g. something like “github.com-prof” or “github.com-dedicated“.

To store the generated private keys to the keychain, first, you need to delete the existing identities added previously from the ssh agent. Run the command below to do this:

ssh-add -D

Now, add the generated private keys for the two accounts to the keychain

sh agent
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_personal
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_work

To list the fingerprints of all identities currently added to the SSH agent and ensure that the private keys have been added as identities, run the command:

ssh-add -l

Now if you reboot your system then the added private keys will be loaded to the ssh-agent automatically from the keychain.

To test that the SSH authentications are set up correctly for both the GitHub accounts, run the below commands one by one:

ssh -T [email protected]
ssh -T [email protected]

At this point, most of the heavy lifting has been done at the machine end, but still, the connections have not been configured and established between the machine and Github. To do it, follow the below steps for each of the Github accounts:

Step 1: Log in to https://github.com/ and open the Github Account
Step 2: Go to "Settings"
Step 3: Click on the "SSH and GPG keys" link on the left-hand side
Step 4: Click on the "New SSH key" button
Step 5: Give it any "Title"
Step 6: Keep the "Key type" as it is
Step 7: Now enter the public key value (stored in your machine e.g. inside the file id_ed25519.pub) in the "Key" field
Step 8: Click on the "Add SSH key" button

… and the connections have now been configured and established.

You will now be able to perform all the operations like git clone, push, pull but there is a small tricky part still left here.

As per the config that you have set before, for the personal account it’s alright but for the work account you always need to use “[email protected]” as the host instead of “[email protected]“. For example, if you want to clone a repo from the work account, you need to clone like below:

git clone [email protected]:abc/project.git

Hope the process has now become clear to you and you are able to configure and use without any difficulty.

If you found this blog interesting, you may also love my other blogs on Git and Github:

  1. GitHub CLI – A complete guide covering all the essential commands
  2. A Deep Dive into how Git works internally in your local

Keep Learning, and Keep Sharing.