Juggling Git with multiple accounts

We’re finally getting a Git server setup at work. Since I also happen to do a bit of open source contributing at work (usually via submitting patches for issues that effect us) and virtually every open source project and its brother is moving to Github lately, I could already see juggling these two systems would be an issue. I can’t use the same SSH key for both our internal Git server and Github for various reasons, and I wouldn’t I want to, really. Fortunately, it turns out managing multiple accounts with Git, and SSH in general, isn’t too bad.

I’ll walk you through doing this, assuming you don’t already have an SSH key or Github account (if you do, though, it’s no biggie, just make sure to back everything up and swap out a few obvious steps below).

  1. Sign up with Github.
  2. Download & install msysgit (chose to use the OpenSSH instead of Tortoise/Putty’s PLink when prompted).
  3. Open a Git Bash prompt (an icon is placed in the Start menu during msysgit’s installation).
  4. Create your SSH key pairs, doing the following steps once for each email/account (internal work & external public ones, for instance):

    1. ssh-keygen -t rsa -C "public_or_private@email.com"
    2. When it asks for the file to save to, specify the same folder it’s suggesting for the default one (i.e. /c/Users/dmozingo/.ssh/) but name the file something different, based on whether it’s for the public or private key (maybe something like id_rsa_github and id_rsa_work).
    3. Enter a passphrase. I usually leave it blank as other guides suggest, trusting (perhaps mistakingly?) Window’s folder security to keep it safe for me.
  5. Navigate to the folder where you just created your SSH key pairs, and create a file named config.
  6. Open the file in notepad and enter the following:

    Host github.com
    	HostName github.com
    	User git
    	PreferredAuthentications publickey
    	IdentityFile /Users/dmozingo/.ssh/id_rsa_github
    
    Host work
    	HostName workGitServername
    	User dmozingo
    	IdentityFile /Users/dmozingo/.ssh/id_rsa_work
    

    You’ll have to do a few substitutions in the file, mainly for the work HostName and both section’s IdentityFile‘s, if they’re different for your setup. The first line of each section specified the name you’ll use in “addressing” your Git commands later. By keeping the first one github.com, you won’t have to change anything when you follow along with other guides online.

  7. Now give GitHub the contents of your public/GitHub SSH key file (the id_rsa_github.pub from my example). You can do this on your account settings page.
  8. Still at the Git Bash prompt, enter ssh-agent.exe bash to start the key manager. You’ll have to do this each time you use Git, unless someone (please?) can tell me a better way.
  9. Enter ssh-add.exe ~/.ssh/id_rsa_github, where the tilday (~) represents your home directory (C:\Users\username for Vista/7, C:\Documents and Settings\username for XP). This should point to the public SSH key file you made earlier. You’ll also have to do this each tiem you use Git, again, unless someone can set me straight here.

That should be it! Test it out by entering ssh git@github.com. You should see something like this:

Github SSH success

So while it’s definitely harder than not having to juggling multiple accounts, it’s not as bad as it sounds. Know a better way to do this? Please let me know!

5 Responses

  1. VirtualStaticVoid Says:

    Hi

    It is possible not to get prompted. Simply change the host name on the repository path to map to one of your configuration entries.

    e.g.

    If your repository Url is git@github.com/dmozingo/somerepos.git

    And given a config entry:

    Host github-userx
    HostName github.com
    User userx
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_userx

    Then change the Url to git@github-userx/dmozingo/somerepos.git

    Checkout http://github.com/guides/multiple-github-accounts for more information.

    Regards
    vSv

  2. willem Says:

    Hi Darrell,

    Thanks for providing me with the last piece of the puzzle.
    With regard to your points 8 & 9 I use a bash script that i found online. The earliest version that i know of is at: http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html

    By default it looks for all keys in your default .ssh directory, but i specified it to load specific keys. Something as good as keychain doesn’t exist on windows and all putty/plink/pageant solutions gave me a lot of problems. With this approach I can at least use openssh keys. I still need to put in my passphrases every time i restart my system but they remain in memory as long as you exit Git for windows/msysgit properly with either the exit or logout command. Anyway, here’s the script, place it in a file bash.rc in your home directory:

    SSH_ENV=”$HOME/.ssh/environment”
    function start_agent {
    echo “Initializing new SSH agent…”
    /usr/bin/ssh-agent | sed ‘s/^echo/#echo/’ > “${SSH_ENV}”
    echo succeeded
    chmod 600 “${SSH_ENV}”
    . “${SSH_ENV}” > /dev/null
    /usr/bin/ssh-add $HOME/.ssh/id_rsa_github;
    /usr/bin/ssh-add $HOME/.ssh/id_rsa_work;
    }
    # Source SSH settings, if applicable
    if [ -f “${SSH_ENV}” ]; then
    . “${SSH_ENV}” > /dev/null
    #ps ${SSH_AGENT_PID} doesn’t work under cygwin
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
    start_agent;
    }
    else
    start_agent;
    fi

  3. willem Says:

    PS Note that the quotes in the above snippet got screwed up.

  4. Darrell Mozingo Says:

    @willem: Thanks for the tip!

  5. Vern Peasant Says:

    So I simply declare you actually come up with a number of outstanding points and I will submit a number of suggestions to add to shortly.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.