Hi, I'm experiencing an issue with posh-git. I'm coming from Linux and this is the first time I try to use Git on Windows (specifically in a Powershell session) so I'm not familiar with how Powershell works, how to configure it and whatnot.
I installed posh-git and managed to make it permanent so that it gets loaded automatically whenever I open a Powershell session. I tried it for a while, making a few commits and it works fantastically, much faster and more responsive than MinTTY. The problems came when I had to push my commits.
I authenticate myself on my remote repository using an SSH key. I saw that Git on Windows comes with a handy Batch script that runs an ssh-agent in the background. Whenever I run it however, the git prompt is gone! The Powershell prompt seems like it's reverted to its default settings. I'll show some screenshots.
This is as soon as I start my Powershell (I've not hit Enter yet):
and then as I hit Enter:
Interestingly enough though, if I type exit I'll get the Git prompt back (screenshot below), so it looks like start-ssh-agent.cmd runs in a sub-shell, which doesn't get the Git prompt.
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.
@rkeithhill thanks for the useful info! Can I start the agent only when I need to? Will this work?
Get-Service ssh-agent | Start-Service
I don't want it to start on every boot, I rarely use Git on Windows.
Yes but you need to do that from PowerShell running elevated. And you can simplify that to Start-Service ssh-agent.
@rkeithhill on Linux though, it works that you need to add your private key to the agent whenever you're going to use it. Your key only gets cached into the agent, you're not adding anything permanently.
Is this different on Windows? You said "without having to re-enter your passphrase".
Yes, it is different in Windows. Windows stores your passphrase (encrypted) using DPAPI (data protection API) - I think. So it is remembered across reboots without you having to re-enter it. That store is local to your machine and is only accessible via your user account on that machine.
The Start-Service thing doesn't work. What am I doing wrong?
C:\Windows\system32> Start-Service ssh-agent
Start-Service : Service 'OpenSSH Authentication Agent (ssh-agent)' cannot be started due to the
following error: Cannot start service ssh-agent on computer '.'.
At line:1 char:1
+ Start-Service ssh-agent
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [S
tart-Service], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand
It's an elevated Powershell session, and ssh should be available:
C:\Windows\system32> Get-Command ssh | Format-List
Name : ssh.exe
CommandType : Application
Definition : C:\Windows\System32\OpenSSH\ssh.exe
Extension : .exe
Path : C:\Windows\System32\OpenSSH\ssh.exe
FileVersionInfo : File: C:\Windows\System32\OpenSSH\ssh.exe
InternalName:
OriginalFilename:
FileVersion: 7.6.0.0
FileDescription:
Product: OpenSSH for Windows
ProductVersion: OpenSSH_7.6p1 for Windows
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language: English (United States)
Make sure you run PowerShell as Adminstrator. On Linux, you'd have to use sudo to start/stop services. Similar on Windows but without sudo.
I solved it! 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.
Ah yeah, by default that service is disabled. So you can't start it until you changed the startup type away from disabled. So manual works as does automatic (and anything else other than disabled).
Closing this issue. If you think it shouldn't be, you can re-open it.
Most helpful comment
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 atC:\Windows\System\OpenSSH\ssh.exe. If you don't get any result, openApps & Featuressection of the Windows 10 Settings app and press the "Manage optional features" link. Once that is open selectOpenSSH Clientto add that feature. After that has finished, open PowerShell again and see ifget-command ssh.exereturnsC:\Windows\System\OpenSSH\ssh.exeNow let's start the ssh-agent service and configure it to automatically start upon reboot. Open PowerShell as administrator and execute:
Cd to your
$home\.sshdir and executessh-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.