I wanted to test out pull requests triggering Travis-ci. I figured I’d use an old account to fork, edit and create a pull request for one of my repos. I had set the local config before adding the remote and pulling, but when I tried to push, I got denied based on my global GitHub username. Turns out I just needed to add the second key to ssh-agent, edit my ssh config file and make a small adjustment to the local git config file.
Adding your key to ssh-agent prompts for any private key password and ssh-agent should use that and not prompt you for it when connecting elsewhere with SSH or SCP.
You can list all keys loaded in ssh-agent
ssh-add -l
Add the second key if needed
ssh-add ~/.ssh/second_key
Edit your SSH config file
vi ~/.ssh/config
Create an alias for github.com that will use the second ssh key
#Second Github Account Host github.com-alt HostName github.com User SecondUserName IdentityFile ~/.ssh/second_key
Setup local git config as usual, just don’t use –global
git config user.name 'Second Name' git config user.email [email protected] git config github.user SecondUsername
and in .git/config, change github.com to the alias set previously
[remote "origin"] url = [email protected]:secondUserName/project.git
Now running git push origin master works as expected.