How do I push to two different GitHub accounts using SSH keys in Powershell?

Viewed 217

I'm on Windows 10 and I have Git for Windows installed. I'm pushing code to two different GitHub accounts, and I'm trying to set up my SSH agent to use two keys (one for each account).

Here's my ~\.ssh\config:

Host github.com-userone
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa

Host github.com-usertwo
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_usertwo

It works correctly if I push/pull using Git Bash, but I want to be able to use Powershell also. When I try to do a push or pull from Powershell, I get the "please make sure you have the correct access rights" error. If I run start-ssh-agent.cmd from Powershell, then git push works for that instance of Powershell.

If I add start-ssh-agent.cmd to my profile.ps1 profile then the SSH agent starts and git push works correctly, but that seems to break other aspects of my Powershell profile (shown below). The Import-Module and Set-Theme no longer work. I don't get any errors; I just no longer get my posh-git or oh-my-git styling.

Import-Module oh-my-posh
Import-Module posh-git
Set-Theme Paradox
Set-Location "c:\users\brubin\documents\visual studio 2017\projects"
start-ssh-agent.cmd

Most of the instructions that I've found for getting this to work involve running eval $(ssh-agent -s), however eval doesn't work in Powershell.

How do I set this up so that I can push/pull from two different GitHub accounts using Powershell?

1 Answers

First, you need ssh-agent only if you have protected those private keys with a passphrase when creating them.

Second, make sure to use the right SSH URL form, referencing those ~/.ssh/config entries:

github.com-usertwo:<usertwo>/myRepo

As mentioned in dahlbyk/posh-git issue 640

Rather than use Git's start-ssh-agent.cmd, I would use the built-in OpenSSH you have on Windows 10 1803.
To use this follow these steps:

From a PowerShell window run get-command ssh.exe. You should see that ssh.exe is located at C:\Windows\System\OpenSSH\ssh.exe.
If you don't get any result, open Apps & Features section of the Windows 10 Settings app and press the "Manage optional features" link. Once that is open select OpenSSH Client to add that feature. After that has finished, open PowerShell again and see if get-command ssh.exe returns C:\Windows\System\OpenSSH\ssh.exe

Now let's start the ssh-agent service and configure it to automatically start upon reboot.
Open PowerShell as administrator and execute:

Get-Service ssh-agent | Set-Service -StartupType Automatic -PassThru | Start-Service

Cd to your $home\.ssh dir and execute ssh-add .\<private-key-file> to add your private key file (and passphrase) to ssh-agent.
Last step is to get Git for Windows to use the WIndows native ssh.exe.
To do that, execute: git config --global core.sshCommand C:/WINDOWS/System32/OpenSSH/ssh.exe.

That's it! Now ssh-agent is running and serving your passphrase. It will continue to do so after a reboot (without having to re-enter your passphrase).
And even better, ssh-agent will work for apps (like Visual Studio) that you start from the Start menu instead of the PowerShell console - which you would have to do it you used start-ssh-agent.cmd.

Note:

I typed "Services" in the Start Menu and I got a window listing all the services currently installed on the machine.
I noticed that, next to "OpenSSH Authentication Agent", it said "Stopped".

I ran Get-Service ssh-agent | Set-Service -StartupType Manual -PassThru and then Start-Service ssh-agent and it worked.
It seems like the service was disabled or something. I've never touched any of these things on Windows, maybe it comes as "Stopped" by default.

Related