Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Pr1meSuspec7/57e8a12ff218e37f8830b033115bdaa7 to your computer and use it in GitHub Desktop.

Select an option

Save Pr1meSuspec7/57e8a12ff218e37f8830b033115bdaa7 to your computer and use it in GitHub Desktop.
Using multiple github accounts with ssh keys

Problem

I have two Github accounts: Pr1meSuspec7 (personal) and palmierimarco (for work). I want to use both accounts on same computer (without typing password everytime, when doing git push or pull).

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):

    # Default github account: Pr1meSuspec7
    Host github.com
       HostName github.com
       IdentityFile ~/.ssh/Pr1meSuspec7_private_key
       IdentitiesOnly yes
       
    # Other github account: palmierimarco
    Host github-palmierimarco
       HostName github.com
       IdentityFile ~/.ssh/palmierimarco_private_key
       IdentitiesOnly yes
    

    NOTE: If you use any account frequently, you should use the default hostname (github.com).

  3. Add ssh private keys to your agent:

    $ ssh-add ~/.ssh/Pr1meSuspec7_private_key
    $ ssh-add ~/.ssh/palmierimarco_private_key
  4. Test your connection

    $ ssh-keyscan github.com >> ~/.ssh/known_hosts
    $ ssh -T git@github.com
    $ ssh -T git@github-palmierimarco

    If everything is OK, you will see these messages:

    Hi Pr1meSuspec7! You've successfully authenticated, but GitHub does not provide shell access.
    Hi palmierimarco! You've successfully authenticated, but GitHub does not provide shell access.
  5. Now all are set, you need remeber

    git@github-palmierimarco:palmierimarco/project.git => user is palmierimarco
    git@github.com:Pr1meSuspec7/project.git.     => user is Pr1meSuspec7
    
  • If you need clone a repository, just do:
$ git clone git@github-palmierimarco:palmierimarco/project1.git /path/to/project1
$ cd /path/to/project1
$ git config user.email "palmierimarco@example.com"
$ git config user.name  "palmierimarco"
  • If you already have the repo set up, after the ssh config instructions, you need change the URL of origin, just do:
$ cd /path/to/project2
$ git remote set-url origin git@github-palmierimarco:palmierimarco/project2.git
$ git config user.email "palmierimarco@example.com"
$ git config user.name  "palmierimarco"
  • If you are creating a new repository on local:
$ cd /path/to/project3
$ git init
$ git remote add origin git@github-palmierimarco:palmierimarco/project3.git
$ git config user.email "palmierimarco@example.com"
$ git config user.name  "palmierimarco"
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin master

Done!

Addon:

#!/bin/bash

# silent prompt
read -p 'GIT profile: ' profile

# switch
case $profile in
  palmierimarco)
    git config user.email "palmierimarco@example.com"
    git config user.name "palmierimarco" 
    git config user.signingKey "palmierimarco_gpg_public_key"
    ;;
  Pr1meSuspec7)
    git config user.email "Pr1meSuspec7@example.com"
    git config user.name "Pr1meSuspec7" 
    git config user.signingKey "Pr1meSuspec7_gpg_public_key"
    ;;
  # default case: raise error
  *)
    >&2 echo "ERR: Unknown profile: $profile"
    exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment