Let’s say we have two github accounts named “shrikant-home” and “shrikant-work”.
I would like to access both accounts from ssh.
I hope you have already created public and private keys for both github accounts and added in the ssh settings as mentioned at http://help.github.com/mac-set-up-git/
Now create a file named ~/.ssh/config file and make following entries
Host shrikant-home.github.com HostName github.com User shrikant-home PreferredAuthentications publickey IdentityFile ~/.ssh/shrikant-home Host shrikant-work.github.com HostName github.com User shrikant-work PreferredAuthentications publickey IdentityFile ~/.ssh/shrikant-work
In above entries we made two different aliases “shrikant-home.github.com” and “shrikant-work.github.com” for same HostName github.com. This change is very important for rest of the remaining changes.
Now add the private keys “shrikant-home” and “shrikant-work” with ssh-add command as follows
$ cd ~/.ssh $ ssh-add ./shrikant-home $ ssh-add ./shrikant-work
The listing of ~/.ssh directory is as follows for clarity:
$ ls config shrikant-home shrikant-home.pub shrikant-work shrikant-work.pub known-host
Let’s say we have “project-home” repository for “shrikant-home” github account and “project-work” repository for “shrikant-work” github account. You need to do the “git remote add” to alias host names with related github account id as follows:
$ git remote add <github account id> git@<related github host alias>:<github account>/projectname.git
Here’s an example for “project-home” repository
$ cd project-home $ git remote add shrikant-home firstname.lastname@example.org:shrikant-home/project-home.git
In order to do any commit in this project, you’ll have to use following “git push” command after “git add” and “git commit”
git push <user-name> master
So in above case it will be:
$ git push shrikant-home master
So to reiterate and for the sake of clarity if you want to push changes to “project-work” repository, you need to do “git remote add” with “shrikant-work” userid with related host alias “shrikant-work.github.com” followed by “git push shrikant-work master”