Try Tuts+ Premium, Get Cash Back!
Quick Tip: How to Work with GitHub and Multiple Accounts
videos

Quick Tip: How to Work with GitHub and Multiple Accounts

Tutorial Details
  • Tools Discussed: Git, GitHub, SSH
  • Difficulty: Moderate
  • Screencast Length: 5 Mins.

So you have a personal GitHub account; everything is working perfectly. But then, you get a new job, and now need to have the ability to push and pull to multiple accounts. How do you do that? I’ll show you how!


Prefer a Screencast?

Choose 720p for the best picture.

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_nettuts.


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/id_rsa_COMPANY.pub. 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
Host github.com
  HostName github.com
  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
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_COMPANY

This time, rather than setting the host to github.com, 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 git@github.com, 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.

Be sure to refer to the screencast if you need a more visual overview of the steps above!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.mikhailkozlov.com Mikhail

    Jeffrey, I always wonder how you get your timing that good.
    Been using organizations for a bit now, but recently needed just what describe. thanks

  • http://www.iamonly4u.com Arun

    Hey Jeffery can you please upload a screencast on php and pdf files?? Something like a invoice generator?

  • http://arlocarreon.com Arlo Carreon

    What is the main difference between a seperate account for a company/client and an “Github Organization”?

    From the little I know, Github organizations lets you associate your existing account with that company/client name. The company/client aren’t ever a separate user account, just an organizational unit where ownership can be passed around. Kinda like facebook pages.

    When is an actual separate account more useful for companies/clients?

  • http://jhu-ror.heroku.com Kalman Hazins

    Good post.

    I personally found that a much simpler way of doing things is by using the https protocol with github as is described in this post https://github.com/blog/642-smart-http-support. I understand that git/ssh way of doing things might be a bit more efficient, but I never had any problems with the https approach.

    -Kalman Hazins
    http://jhu-ror.heroku.com/lecture_slides/Lecture1-Git-and-Github.pdf

  • http://alpheus.me/ Craig Ludington

    A summary in the introduction would have helped. For example:

    “By creating different host aliases to github.com in your ~/.ssh/config,
    and giving each host alias its own ssh key, you can easily use multiple
    github accounts without confusion.

    That’s because github.com distinguishes not by user, which is always just git,
    but by the ssh key you used to connect.

    Just configure your remote origins using your own host aliases.”

  • http://www.github.com/dotink/ Matthew J. Sahagian

    Why would you have multiple GitHub Accounts? Aside from an Organization (which isn’t really an account in the proper sense since you don’t sign into it directly).

    One of the major benefits of the way GitHub does things is the fact that I can be added as a private collaborator to all the closed source companies I work for. Why wouldn’t they simply add my existing user to their organization?

    • http://matthewpskelton.net/ Matthew Skelton (@matthewpskelton)

      Some people (like me) have separate accounts for work and private use, or perhaps for different clients.

      The advice here is spot on – thanks, Jeremy.

    • Rich Wheadon

      This doesn’t alias you as different users, it just allows you to generate a unique key for interacting with different accounts. You continue as your official github account identity without sharing your personal key with others.

      Thanks Jeremy… this allows me to segregate my external repo ids quite nicely.

  • Charles Wang

    I actually just did this the other day and I had to look it up on the Github docs, but you provided a wonderful screencast. I think one of the things that helped me a lot when debugging issues was trying something like “ssh -vT git@github.com” or “ssh -vT git@github-client” depending upon how you set up your hosts.

  • http://coderbay.com/ coderbay

    worth reading.

  • http://www.davidsafar.com/ David Safar

    Many thanks for this. I’ve been searching for a way to do this for about 45 minutes — it appears that there used to be a doc on the GitHub help pages about this, but it no longer exists. This article was a life-saver!

  • Shuky

    A nicer way to go I think is with this gem,

    https://github.com/walle/gas

    Give it a try.

  • Erin Brown

    Thank you for this tutorial. Very helpful!

  • deadshox

    Hey. Thanks a lot for this. It was very helpful. :)

  • http://joshdiehl.com Josh Diehl

    Thanks, just what I needed for my github work//home situation.

  • Mike V

    I’ve watched this video and followed all the SSH steps (as well as followed other tutorials on this topic which all say pretty much the same thing), but every time I try to “git push -u origin master” from my secondary account’s git repo, my Mac instead sends my primary SSH key. Git then responds with

    ERROR: Permission to SecondaryAccount/testing.git denied to PrimaryAccount.

    I don’t see what I’m doing wrong…?

  • http://deepumohan.com Deepu Mohan Puthrote

    If on windows, first run
    ‘exec ssh-agent bash’
    and then
    ‘ssh-add ~/.ssh/id_rsa’

  • http://deepumohan.com Deepu Mohan Puthrote

    If on windows, first run
    ‘exec ssh-agent bash’
    and then
    ‘ssh-add ~/.ssh/id_rsa’

  • http://happytesting.wordpress.com Sigge

    If you have problem with the ssh-add command, I found the solution here:
    http://funkaoshi.com/blog/could-not-open-a-connection-to-your-authentication-agent

  • ryan

    Thank you! You saved me many head aches! :)

  • rohit

    +1 Great!!

  • Dilip Gurung

    Thanks Jeff. The video just made my life so easy toady.

  • Nitish Kansal

    Hi,
    This is a great tutorial. It almost solve my problem but i am having a doubt. i have one company account which is working perfectly fine but i have created another account with my personal email id. And i want to use both accounts on same computer but as you have mentioned we have to attach new ssh key i have generated one but in which account i have to attach it i mean in company account i have to attach personal ssh key or in personal account i have to attach personal ssh key. Please help me in this
    Thanks,
    -Nitish Kansal

  • http://tomiford.com Michael

    After pushing commits to my company project the git author on the commits is my personal account, not my company account. I have been using:

    $ git pull git@github-company:company/repo.git
    $ git push origin master

    How do I specify the correct author for the commit?

    • http://twitter.com/zonabidesign jose gomez

      this happened to me too, any resolution?

    • http://twitter.com/zonabidesign jose gomez

      figured out a solution: you can edit the .git/config file within a specific repo to use certain author/emails. but, you have to do that for each repo that you want different credentials than your global git config

  • AK

    DOES. NOT. WORK. Be EXTREMELY careful. This setup allows one of my accounts to push to a repository THAT IT IS NOT EVEN A COLLABORATOR ON.

  • http://blainegarrett.com Blaine Garrett

    If you are using sourcetree, it should be noted that you need to follow these same steps. However, because of the way the git permissions work (some might argue whacky permissions) you need to additionally edit the config for each repo or else you risk committing under the global user even if you were able to create a new remote just fine using the Host tweak above.

    cd into your repo and run:
    $ git config user.email -> tells you which user you will be committing as
    If this is wrong, run
    $ git config user.email “email@of.second.account”

    Back in source tree, commit and push as normal. You should now be committing as the intended (non-global user).

    Final note, I had to do the same steps per submodule as well.

  • http://openingdesign.com Ryan Schultz

    If ssh-add says “Could not open a connection to your authentication agent.”, then you don’t have a SSH agent running. Launch one using this command:

    eval $(ssh-agent)

  • http://twitter.com/a_schacherbauer Andi Schacherbauer

    Great tutorial!
    I just have one question left: If i commit something to my work account, my avatar and of my private acount appears (right at the commit message, in the upper right corner, my private acount avatar is shown). Is there a way to specify from which user the commit was done?

  • http://twitter.com/zonabidesign jose gomez

    great tutorial. now, how can one use multiple git accounts with the github for mac client?

  • Enrique Tuya

    Thanks for the post Jeffrey. Very useful!

  • Nick Joost

    Why are both commits (to the personal acct, and to the work acct.) done by the same user (Jeffrey Wey). I thought that the point was to be able to commit to a repo as two different users from the same machine? I was hoping to be able to totally separate my work and personal account, but after going through this tutorial I get the same result — the commits are all from my work account, even if I can commit to both personal/work repos.