Set up multiple SSH identities on Git

As a freelance developer I get to work with different clients and contractors and it’s common to find that they use different repository hosting services; Github, Codebase, Bitbucket, etc. Today I’ll share my experience configuring Git in order to work with multiple repository hosting providers.

Besides, I had a pretty bad time with errors like “Permission denied (publickey).” and “Could not open a connection to your authentication agent.“.

Default identity

The default identity should be the one you use for your personal projects or work or just the one you use the most. There are ways to configure it and almost every provider has the proper instructions in how to set it up (Github, Bitbucket, Codebase).

As you can see they’re almost the same and that experience will come in handy in just a second.

New identities

Now we need new identities because sometimes we’re given a new email address in order to work with that new client or just need to create a new identity in order to sync with a new repository hosting provider.

Step 0: Change your config name and email (if necessary)

You gotta keep this always in mind when making commits and pushing to foreign repositories (not the default one) and change them back after pushing to remotes.

 Step 1: Generate a new SSH key

 Step 2: Create or modify the SSH config file

Now, suppose we have three different identities for each of the providers I named above (Github, Bitbucket and Codebase), so we have the following files in the directory ~/.ssh/

  • bitbucket_rsa
  • bitbucket_rsa.pub
  • codebase_rsa
  • codebase_rsa.pub
  • github_rsa
  • github_rsa.pub

Modify the file using the following format

Given the files I named above, the file looks like this

Save it and that’s it. It should work now as expected for those different repository hosting providers you configured (or at least that’s what’s worked for me).

0 Comments

    Leave a Reply