Taking Notes

cause i'll forget

Github With Two+ Accounts


Since writing this article, I’ve been fiddling with variations on how to best handle multiple accounts. I still use the .ssh config and ssh:// style git URLs, but I’ve found that the simply dumping all my ssh identities and adding back just the one I’m going to be using during the current task is actually a pretty passable solution.


ssh-add -D
ssh -T -i ~/.ssh/id_rsa_personal git@github.com

to switch over to my personal account. It wouldn’t work if I was switching twice an hour, but it’s fine when I’m generally using one account or another for days at a time.

Managing github with two accounts is not obvious. You’re connecting to the same host, but sometimes you want to use one identity and somethings the other.

This is a balancing act of git configuration and ssh configuration, but it’s not as bad as it might seem, once you’re set up.

Setup your accounts

Create and/or configure the two github accounts you wish to configure

We’ll refer to them as

  • GithubPersonal
  • GithubWork

and make sure they’ve each got their own ssh keys configured. (Github has great tutorials on how to generate keypairs and associate them with your account.)

We’ll assume the keys are

  • ~/.ssh/id_rsa_personal & ~/.ssh/id_rsa_personal.pub
  • ~/.ssh/id_rsa_work & ~/.ssh/id_rsa_work.pub

Configure SSH

If you don’t have one already create a file called config at ~/.ssh/config

I use textmate, but you could use any text editor.

mate ~/.ssh/config

We’ll be setting up config to use aliases when connecting to github using an alias. For example I can setup an alias called work to turn

ssh -i ~/.ssh/id_rsa doug@server.example.com


ssh work

Our goal is to use aliases to tell git what keypair to use when connecting to github.com

There’s some flexibility in how you setup the config, you need to set up an alias for at least one of the accounts. I like to alias work and leave my personal account untouched, but a more thorough invidual might do both. This will become clearer as we proceed.

Host personal
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal
Host work
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_rsa_work

Now we’ll get the keypairs loaded so the system will use them

# Clear out currently stored identities
ssh-add -D
# Add in personal key (you may be prompted for your keypairs password)
ssh-add ~./ssh/id_rsa_personal
# And work
ssh-add ~./ssh/id_rsa_work
# Print a list of loaded keys to make sure both are loaded
ssh-add -l

Now you’ve got your keys in place, let’s poke github and see if it recognizes us

ssh -T personal

Should give you

Hi GithubPersonal! You’ve successfully authenticated, but GitHub does not provide shell access.

Same for work

ssh -T work

Should give you

Hi GithubWork! You’ve successfully authenticated, but GitHub does not provide shell access.

So now we’re up and running and doing the ssh tango, onto the next one.

Configure Git

Git can be configured with a username, email address and custom fields. Github recommends using the custom fields to add in your github username and your github token.

# Check your current globals
git config --global -l

They should look something like

user.name = Some Guy

user.email = someguy@example.com

core.excludesfile = /Users/someguy/.gitignore

mergetool.keepbackup = true

github.username = GithubPersonal

github.token = 123456789asdjfhlkjhdflkh8598

I choose to configure my globals to use my personal account, becaues I have a lot more active repos in my personal account than my work account.

You can configure any of the setting with the following syntax

git config --global user.name "Some Guy"

by swapping out the name of the property and it’s value.

I recommend setting user.name, user.email, github.username, and github.token.

Now you’re configured for general use. We’ll revisit local configs after we cover cloning repos.

Getting your code

Normally you’d go to the github project site and grab the clone URL and be off to the races.

Something like

git clone git@github.com:username/repo.git

and if you have permission you can push/pull/etc from that repo.

In this case we need to tell github what we’re doing so we change the syntax some

git clone ssh://work/username/repo.git

would pull down the repo using the ssh keypair for you work account.

The logic is the same to use your personal account

git clone ssh://personal/username/repo.git

Git Config Local

Now that you’ve got some code to work with, the last thing to fix is making sure that your commits show up on github accredited to the right account. Git will automatically use the global config for any keys you don’t override on a per repo basis.

You do have to do this for each repo you clone, but only once per repo.

cd repo
# Check your current local config
git config --local -l

core.repositoryformatversion = 0

core.filemode = true

core.bare = false

core.logallrefupdates = true

core.ignorecase = true

remote.origin.fetch = +refs/heads/:refs/remotes/origin/

remote.origin.url = ssh://work/username/repot.git

branch.master.remote = origin

branch.master.merge = refs/heads/master

Again I recommend setting user.name, user.email, github.username, and github.token, but this time use your work account.

You’ll end up with a few new lines in your local config when you’re done

user.name = Some Guy

user.email = workemail@example.com

github.username = GithubWork

github.token = 3235435468413asdsfdasdfsfs

All Done

That’s the whole show. You can now configure aliases and local configs to make sure the right projects are using the right accounts and right metadata.

Hope this is useful.