I've been using the following setup for a while to switch between multiple ssh keys and github accounts.
Firstly I have two keys in .ssh/
id_rsa_work
id_rsa_work.pub
id_rsa_personal
id_rsa_personal.pub
And a config for those keys
Host personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
Host work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
And then I'm able to switch between accounts by doing
ssh -T work or ssh -T personal
and then I use two gitconfig files to auto pickup the git config.
~/.gitconfig
[user]
name = hex6b
email = [email protected]
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
~/work/.gitconfig
[user]
name = My work name
email = [email protected]
The work setup seems to work fine, however the personal one will not allow me to clone new repos or push/pull existing repos.
ERROR: Repository not found. fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
Not sure why it's suddenly not working, maybe since github introduced personal access tokens.
I can also see both keys when I do a ssh-add -l
so I don't need to add them to the ssh agent.
Do I need to do anything special with the urls from github? Usually I just copy them from the code dialog.

Yes, you do need to change the URLs. The Git FAQ explains how to do this by changing the URL in each case.
The example configuration they give looks like this:
# This is the account for author on git.example.org.
Host example_author
HostName git.example.org
User git
# This is the key pair registered for author with git.example.org.
IdentityFile ~/.ssh/id_author
IdentitiesOnly yes
# This is the account for committer on git.example.org.
Host example_committer
HostName git.example.org
User git
# This is the key pair registered for committer with git.example.org.
IdentityFile ~/.ssh/id_committer
IdentitiesOnly yes
In this case, the hostname would be example_author or example_committer instead of git.example.org.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With