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!

Add Comment

Discussion 19 Comments

  1. Mikhail says:

    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

  2. Arun says:

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

  3. Arlo Carreon says:

    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?

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

  5. 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.”

  6. 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?

  7. Charles Wang says:

    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.

  8. David Safar says:

    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!

  9. Shuky says:

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

    https://github.com/walle/gas

    Give it a try.

  10. Erin Brown says:

    Thank you for this tutorial. Very helpful!

  11. deadshox says:

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

  12. Josh Diehl says:

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

  13. Mike V says:

    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…?

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

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

  16. Sigge says:

    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

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.