Adding a Second GitHub Account to a Computer

Posted on:

Tagged as: .

Recently I needed to commit to a repo under a different account, thinking this would be a common issue meant I was quite surprised to discover that GitHub Desktop doesn’t support adding multiple accounts. Reading up on the subject I got it working using SSH keys, I’ve documented the process below.

Install Command Line Git and Setup GitHub Desktop

I will not document this here but you should have your main account set up and be able to pull/push as normal on the command line and additionally the GitHub Desktop if you’re using it.

Clone the Second Account Repo

Clone the repo from your second account using the command line. You can use either the https or ssh method as we can change it later:

git clone [email protected]:user/repo.git

Create an SSH key

As you will be effectively switching accounts to pull/push to this new repo you need to create a pair of SSH keys, this will be used instead of being repeaatedly asked for a password on the second account.

In your terminal cd into repo folder:

cd repo-folder

Now you can create the SSH keys, this will create two files, one will end in .pub, this is your public key, you will add it into your second GitHub account.

Back in the ternimal app create your key, the email address should be the one on your second GitHub account:

ssh-keygen -t rsa -C "[email protected]"

The prompt will ask you to Enter file in which to save the key (C:\Users\myuser/.ssh/id_rsa) give it a descriptive name, I named mine id_rsa_secondgit.

It will then ask you to Enter passphrase (empty for no passphrase), I opted to not set a passphrase. It will ask you to confirm you choice.

You should then have two new files created: id_rsa_secondgit and id_rsa_secondgit.pub.

Add the Public Key to Your GitHub Account

Login to your GitHub.com account, and go into Settings from the account menu in the top right of the page. Then go into SSH and GPG Keys in the menu on the left of the main settings page.

Click on the New SSH Key button, copy and paste to contents of the id_rsa_secondgit.pub file into the key form textarea. You should give the key a descriptive title, maybe the name of the computer you’re using the key on.

Update the Settings in the Repo Config File

In the folder of your cloned repo you need to edit the .git/config file. In the [user] section you should add the second account’s identity, this is what will be dislayed when you commit a change.

In the [core] section you should add the sshCommand line, adding in the location of your private SSH key (the one that doesn’t end in .pub).

In the [remote] section check the URL setting, if it starts with https rather than [email protected] you should change it to the pattern shown below.

[core]
  repositoryformatversion = 0
  filemode = false
  bare = false
  logallrefupdates = true
  ignorecase = true
  sshCommand = ssh -i ~/.ssh/id_rsa_secondgit
[user]
  email = [email protected]
  name = "Your Name"
[remote "origin"]
  url = [email protected]:user/repo.git
  fetch = +refs/heads/*:refs/remotes/origin/*

...

Finally, if you’re planning to use the GitHub Desktop app add your repo to it, make a small edit and commit and push. You should see your edit on the GitHub.com repo page has been made with your second account.