Using multiple Github accounts with ssh keys
Recently, I had a need to have two Github accounts on the same computer and I wanted to use both of them without typing password everytime, when doing git push or pull. Let’s say that I have two accounts “superman” and “batman”. A solution is to use ssh keys and define host aliases in ssh config file (each alias for an account).
Solution
1.Edit or create ssh config file (~/.ssh/config):
# Default github account: superman
Host github.com
HostName github.com
IdentityFile ~/.ssh/superman_private_key
IdentitiesOnly yes
# Other github account: batman
Host github-batman
HostName github.com
IdentityFile ~/.ssh/batman_private_key
IdentitiesOnly yes
2.Add ssh private keys to your agent link
$ ssh-add ~/.ssh/superman_private_key
$ ssh-add ~/.ssh/batman_private_key
3.Test our connection
$ ssh -T [email protected]
$ ssh -T [email protected]
Warning: Permanently added the ECDSA host key for IP address '140.82.121.3' to the list of known hosts.
Hi superman! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@github-batman
Warning: Permanently added the ECDSA host key for IP address '140.82.121.3' to the list of known hosts.
Hi batman! You've successfully authenticated, but GitHub does not provide shell access.
4.Cloning repository
Using superman account:
$ git clone [email protected]:superman-user/project.git /path/to/project
Using batman account:
$ git clone git@github-batman:batman-user/project-other.git /path/to/project-other
5.Using proper user and email
Assumption that for default account we are using global git configuration, so for not default Github account go to cloned repository and do the following:
$ cd /path/to/project-other
$ git config user.email "[email protected]"
$ git config user.name "Batman"