Ollayor's Blog

Using multiple github accounts with ssh keys

How to...?



  # Default github account: batman
  Host github.com
     HostName github.com
     IdentityFile ~/.ssh/batman_private_key
     IdentitiesOnly yes
     
  # Other github account: superman
  Host github-superman
     HostName github.com
     IdentityFile ~/.ssh/superman_private_key
     IdentitiesOnly yes

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


$ ssh-add ~/.ssh/oanhnn_private_key
$ ssh-add ~/.ssh/superman_private_key


  • Test your connection:
$ ssh-keyscan github.com >> ~/.ssh/known_hosts
$ ssh -T git@github.com
$ ssh -T git@github-superman


If everything is OK, you will see these messages:


Hi batman! You've successfully authenticated, but GitHub does not provide shell access.


Hi superman! You've successfully authenticated, but GitHub does not provide shell access.


  • Now all are set, you need remeber:
git@github-superman:org/project.git => user is superman
git@github.com:org/project.git.     => user is batman


  • If you need clone a repository, just do:
$ git clone git@github-superman:org1/project1.git /path/to/project1
$ cd /path/to/project1
$ git config user.email "superman@example.com"
$ git config user.name  "Super Man"


  • 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-superman:org2/project2.git
$ git config user.email "superman@example.com"
$ git config user.name  "Super Man"


  • If you are creating a new repository on local:
$ cd /path/to/project3
$ git init
$ git remote add origin git@github-superman:org3/project3.git
$ git config user.email "superman@example.com"
$ git config user.name  "Super Man"
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin master


Done! Goodluck!