Work with multiple GitHub accounts on a single machine

Work with multiple GitHub accounts on a single machine

Here's my backstory. Last week, I joined as a Software Engineering Intern at a freight optimization company. They promised to provide a laptop within 1 week until I should work on my personal laptop. By the way, I am using Windows on my machine. I was expected to clone the repos from the organization's GitHub account. I knew that I need to create a new GitHub account with the work email and a new SSH key. Remember I do have my personal GitHub account and ssh key set up on my laptop. I am not sure how Git knows that I am cloning a repo from my personal or work account. Finally, with some searches, I came to know how things work in Git. I am going to share with you all my findings through this post.

Alright, I assume that you already have the ssh key setup for your personal GitHub account. I start by creating the same for the work GitHub account.

ssh-keygen -t ed25519 -C "your_email@example.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\hello/.ssh/id_ed25519): C:\Users\hello/.ssh/id_ed25519_work

Do give a meaningful name for the key as mentioned above.

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

The passphrase is optional. You can skip those using the Enter key.

Your identification has been saved in C:\Users\hello/.ssh/id_ed25519_work.
Your public key has been saved in C:\Users\hello/.ssh/id_ed25519_work.pub.
The key fingerprint is:
SHA256:EolBGh/a+8926LV0YqKqsgyVRwwDMBUe4z2wt5fPXAY roopeshsaravanan.dev@gmail.com
The key's randomart image is:
+--[ED25519 256]--+
|=oXo+            |
| + / + .         |
|  * X o E        |
|   + + o .       |
|  o + + S o      |
| . . o = o       |
|.     . =.= .    |
|+      +o=.+     |
|.+.....o+..      |
+----[SHA256]-----+

Now the key will be generated something like this. Also, you can check the generated keys using the command below in the Git Bash terminal.

ls -al ~/.ssh

The next step is to add the key to the ssh-agent. Start the ssh-agent by,

eval "$(ssh-agent -s)"

Add your ssh private key to the ssh-agent,

ssh-add ~/.ssh/id_ed25519_work

Now, it's time to add the key to GitHub. For that copy the desired key using the command,

clip < ~/.ssh/id_ed25519_work.pub

The above command copies the contents of the id_ed25519_work.pub file to your clipboard.

Navigate to github.com -> settings -> SSH and GPG keys -> New SSH key

Add the appropriate title and paste the key using "Ctrl + V". Then click "Add SSH key" to add the key to your account.

That's all about SSH key creation and addition. Though we need to work with two accounts we need to explicitly tell Git using a config file.

Create a file named "config" in this directory "C:\Users\username\.ssh"

Paste the below text into that file

# Personal
Host personal github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal
  IdentitiesOnly yes
# Work
Host work github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work
  IdentitiesOnly yes

The terms "personal" and "work" mentioned after the "Host" are an alias for key identification. The setup is done.

Let's clone a repo from my personal GitHub account by copying the SSH.

To tell Git that you're cloning a repo from your personal account, you should explicitly mention the alias instead of "github.com" like this,

# Actual
git clone git@github.com:roopeshsn/roopeshsn.git

# Now
git clone git@personal:roopeshsn/roopeshsn.git

I mentioned "personal" instead of "github.com". If you're cloning from your work account mention "work".

Yep, the command works for me. It works for you too!

You can verify the linkage by executing,

git config --local -e
[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = git@personal:roopeshsn/roopeshsn.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

A file will be opened in your editor with text similar to the above. Here check the url field below [remote "origin"].

That's it about setting up Git to work with multiple GitHub accounts. I appreciate you reading this post. If you find this post useful, please like, share, and comment this post with others who might benefit from it.

Did you find this article valuable?

Support Roopesh Sn's Blog by becoming a sponsor. Any amount is appreciated!