You can connect to GitHub using the Secure Shell Protocol (SSH), which provides a secure channel over an unsecured network. Setting up one github account is fairly easy, with multiple accounts setup using different ssh keys gets bit complicated.

Table of Contents

  1. Problem
  2. Solution
    1. How to?

Problem

I have two Github accounts: hshanbhag (personal) and hshanbhag9 (for work). I want to use both accounts on the same computer. Github doesn’t allow to use same ssh public key for both accounts.


Solution

Use ssh keys and define host aliases in ssh config file (each alias for an account).

How to?

  1. Generate ssh key pairs for accounts and add them to GitHub accounts.

  2. Edit/Create ssh config file (~/.ssh/config):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # Default github account: hshanbhag
    Host github.com
    HostName github.com
    IdentityFile ~/.ssh/hshanbhag9_private_key
    IdentitiesOnly yes

    # Other github account: hshanbhag
    Host github-hshanbhag
    HostName github.com
    IdentityFile ~/.ssh/hshanbhag_private_key
    IdentitiesOnly yes
  3. Add ssh private keys to your agent:

    1
    2
    $ ssh-add ~/.ssh/hshanbhag9_private_key
    $ ssh-add ~/.ssh/hshanbhag_private_key
  4. Test your connection

    1
    2
    $ ssh -T [email protected]
    $ ssh -T [email protected]

    With each command, you may see this kind of warning, type yes:

    1
    2
    3
    The authenticity of host 'github.com' can't be established.
    RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:
    Are you sure you want to continue connecting (yes/no)?

    If everything is OK, you will see these messages:

    1
    Hi hshanbhag9! You've successfully authenticated, but GitHub does not provide shell access.
    1
    Hi hshanbhag! You've successfully authenticated, but GitHub does not provide shell access.
  5. Now all are set, just clone your repositories. Then add the name and email address which you want to use for respective repo.

    1
    2
    3
    4
    $ git clone [email protected]:org2/project1.git /path/to/project2
    $ cd /path/to/project2
    $ git config user.email "[email protected]"
    $ git config user.name "Harish Shanbhag"