Multiple GitHub accounts on one computer

I recently came accross a problem that is perhaps more common to than realized. Problem: we need to have multiple identities on github for a number of reasons, e.g. work versus home account.

First, you would need to update the ~/.ssh/config file to specify different “hosts”:

1
2
3
4
5
6
7
8
9
10
11
Host github-work
Hostname github.com
User git
IdentityFile ~/.ssh/id_rsa_work
IdentitiesOnly yes
Host github-home
Hostname github.com
User git
IdentityFile ~/.ssh/id_dsa_home
IdentitiesOnly yes

The above assumes that you have uploaded ~/.ssh/rsa_work.pub and ~/.ssh/rsa_home.pub to your respective github account.

Next, in the project/.git/config file, update the url value to match the above host specified in your configuration file from above. For example

1
2
3
[remote "origin"]
url = git@github-work:company/work-project-name.git
fetch = +refs/heads/*:refs/remotes/origin/*

and

1
2
3
[remote "origin"]
url = git@github-home:personal-account/toy-project-name.git
fetch = +refs/heads/*:refs/remotes/origin/*

Finally, you might have configured global username and email, in which case you should probably have local configuration (defined in .git/config in your project folder):

1
2
3
[user]
name = Your Name
email = your-email@home.org

Update the above, of course, for work projects.

Source: stackoverflow