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
[bash]
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
[/bash]
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
[bash]
$ cd ~/.ssh
$ ssh-add ./shrikant-home
$ ssh-add ./shrikant-work
[/bash]
The listing of ~/.ssh directory is as follows for clarity:
[bash]
$ ls
config shrikant-home
shrikant-home.pub shrikant-work
shrikant-work.pub known-host
[/bash]
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:
[bash]
$ git remote add <github account id> git@<related github host alias>:<github account>/projectname.git
[/bash]
Here’s an example for “project-home” repository
[bash]
$ cd project-home
$ git remote add shrikant-home git@shrikant-home.github.com:shrikant-home/project-home.git
[/bash]
In order to do any commit in this project, you’ll have to use following “git push” command after “git add” and “git commit”
[bash]
git push <user-name> master
[/bash]
So in above case it will be:
[bash]
$ git push shrikant-home master
[/bash]
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”
Leave a Reply