<-- Back to The Cleavr Slice

05 September 2022

blog

How to setup multiple GitHub Accounts on a device?

You may want to configure multiple GitHub accounts for various purposes. A common example is needing to make contributions to both personal and work projects.

In most cases, you’ve already configured your personal account. So, let's jump into configuring the second account for your work account. You’ll first need to create a new SSH key pair to manage multiple GitHub accounts on the same machine.

ssh-keygen -t ed25519 -C “email@example.com” -f ~/.ssh/id_ed25519_work

-C means a comment. It can be helpful to know the email associated with the account.

-f specifies the path to the ssh key

Now, copy your public key and add it to GitHub. I’ll be using pbcopy to copy my public key to the clipboard from ~/.ssh/id_ed25519_work.pub

pbcopy > ~/.ssh/id_ed25519_work.pub

To add your public key to GitHub, go to GitHub settings > SSH and GPG Keys > New SSH Key. Add a title for your new SSH key, paste the key you’ve copied to your clipboard, and then click on Add SSH Key.

GitHub Settings

Add SSH Key

Once you’ve added the key to GitHub let's manage some configs on our device. The SSH config file is located in the .ssh directory. Usually ~/.ssh/config. If the config file is not yet created you can create one using touch ~/.ssh/config.

Update the config file with a new block that defines a relation with the SSH key to your GitHub Account.

#Personal account
Host github.com-githubUser1
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_ed25519_personal
   IdentitiesOnly yes

#Anish Cleavr account
Host github.com-githubUser2
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_ed25519_work
   IdentitiesOnly yes

githubUser1 and githubUser2 are GitHub usernames of the respective account. ~/.ssh/id_ed25519_personal and ~/.ssh/id_ed25519_work are paths to the ssh keys for their respective accounts. You need to update these values accordingly and save the file.

I’ve created a private repository on GitHub from one of the accounts. Let's try cloning the repo from both accounts and see what happens.

Cloning from the account that has access to it:

git clone git@github.com-theanishg:theanishg/example-repo.git

Git Clone

I was able to clone the repo as expected.

As expected I got denied while cloning the repo from another account that doesn't have access to it.

Git Clone

Let's invite this user to the private repo and try cloning it again.

GitHub User Invitation

After accepting the invitation I was able to clone the repo.

Git Clone

Ensure that the remote URL is in the proper format. git@github.com-{{ username }}:{{ repoOwnerUsername/organizationUsername }}/{{ repoName }}.git

While copying the URL from GitHub to clone add your username after git@github.com by separating it with -.

Now, change the directory to the project that you’ve recently cloned and run git config –local -e to open local config file. Add the following user details to it.

user
name = Anish Ghimire
email = anishghimire862@gmail.com

The config file should look somewhat like this:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = git@github.com-anishghimire862:theanishg/example-repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
    remote = origin
    merge = refs/heads/main

I’ve cloned the same repository inside two different directories as two different users. Let’s verify these by making some changes and pushing them from both users.

Git Commits

As you can see from the screenshot above, we were able to make commits to repo from different users. You can now perform all other actions like pull/push/commit etc.

Take control of your servers and deployments. Without breaking a sweat.

Sign up for a 5-day free trial of Cleavr Pro. No credit card required until you decide to subscribe.

Sign up for free