Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to Work with GitHub and Multiple Accounts

Step 1 - Create a New SSH Key

We need to generate a unique SSH key for our second GitHub account.

ssh-keygen -t rsa -C "your-email-address"

Be careful that you don't over-write your existing key for your personal account. Instead, when prompted, save the file as id_rsa_COMPANY. In my case, I've saved the file to ~/.ssh/id_rsa_work.

Step 2 - Attach the New Key

Next, login to your second GitHub account, browse to "Account Overview" and attach the new key, within the "SSH Public Keys" section. To retrieve the value of the key that you just created, return to the Terminal and type: vim ~/.ssh/

Copy the entire string that is displayed, and paste this into the GitHub textarea. Feel free to give it any title you wish.

Next, because we saved our key with a unique name, we need to tell SSH about it. Within the Terminal, type: ssh-add ~/.ssh/id_rsa_COMPANY. If successful, you'll see a response of Identity Added.

Step 3 - Create a Config File

We've done the bulk of the workload; but now we need a way to specify when we wish to push to our personal account, and when we should instead push to our company account. To do so, let's create a config file.

touch ~/.ssh/config vim config

If you're not comfortable with Vim, feel free to open it within any editor of your choice. Paste in the following snippet.

Default GitHub

	  User git
	  IdentityFile ~/.ssh/id_rsa

This is the default setup for pushing to our personal GitHub account. Notice that we're able to attach an identity file to the host. Let's add another one for the company account. Directly below the code above, add:

	Host github-COMPANY
	  User git
	  IdentityFile ~/.ssh/id_rsa_COMPANY

This time, rather than setting the host to, we've named it as github-COMPANY. The difference is that we're now attaching the new identity file that we created previously: id_rsa_COMPANY. Save the page and exit!

Step 4 - Try it Out

It's time to see if our efforts were successful. Create a test directory, initialize git, and create your first commit.

	git init
	git commit -am "first commit'

Login to your company account, create a new repository, give it a name of "Test," and then return to the Terminal and push your git repo to GitHub.

	git remote add origin git@github-COMPANY:Company/testing.git
	git push origin master

Note that, this time, rather than pushing to, we're using the custom host that we create in the config file: git@github-COMPANY.

Return to GitHub, and you should now see your repository. Remember:

When pushing to your personal account, proceed as you always have. For your company account, make sure that you use git!github-COMPANY as the host.


This comment has been minimized.

Copy link

commented Dec 18, 2018

For some reason I had to change the email in the repo to show the commits in the right account.

I made a bash script to replace the with the other host and then change the email to the other account

  git clone "${1/}"
  repo_name_find=$(echo "$1" | egrep -o '/.+\.git')
  #remove slash
  repo_name=$(echo "$repo_name_find" | egrep -o '[^/].+')
  # remove the '.git' and cd in that new folder
  cd "${repo_name/.git/}"
  git config ""

(I am not good at bash, maybe someone can do better)

I use git clone to use my primary account and gitcloneother to clone for the other account.


This comment has been minimized.

Copy link

commented Feb 22, 2019

You might need to add IdentitiesOnly yes if you use ssh-agent or gpg-agent to manage identities.


This comment has been minimized.

Copy link

commented Apr 17, 2019

Hello @JoaquimLey. I'm trying to set my repo to use correct ssh key, but I can't make it.

  1. I edited ~/.ssh/config file like this:
Host github-10gen
    User git
    IdentityFile ~/.ssh/new_key_file
    IdentitiesOnly yes
  1. I set remote url like this:
    git remote set-url origin git@github-10gen:10gen/ops-manager-cloudfoundry
  2. Trying
    git push origin dev
    but getting error:
ssh: Could not resolve hostname github-10gen: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

What am I doing wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.