97

I have git installed and it works great in the command prompt, but when I open up powershell it gives me this warning:

WARNING: Could not find ssh-agent

I have pageant running and loaded with my private key. This works in all the gui tools and the command prompt but not in Powershell.

What's going on?

neontapir
  • 4,698
  • 3
  • 37
  • 52
Khalid Abuhakmeh
  • 10,709
  • 10
  • 52
  • 75
  • 1
    Could the `%PATH%` be different for some reason? – Jonas Heidelberg Sep 19 '11 at 11:46
  • 1
    http://haacked.com/archive/2011/12/19/get-git-for-windows.aspx/ – rohancragg Nov 12 '14 at 11:13
  • 1
    @rohancragg's link essentially says add: `$env:path += ";" + (Get-Item "Env:ProgramFiles(x86)").Value + "\Git\bin"` to the top of your PROFILE file (use the PS command `ise $PROFILE` to open the PROFILE for editing) – Toby Jan 14 '15 at 12:23
  • sorry for not explaining - haacked says: "it was not able to find the “ssh-agent.exe” executable. That file is located in C:\Program Files (x86)\Git\bin. but that folder isn’t automatically added to your PATH by msysgit" – rohancragg Jan 22 '15 at 16:10
  • I would suggest [this answer](http://stackoverflow.com/a/24368751/5351) is now preferable to the advice in the haacked post: – rohancragg Mar 03 '15 at 14:49

6 Answers6

137

For those looking for a detailed explanation have a read of this blog post. Below is a quote from the blog post. Ultimately the ssh-agent.exe needs to be in the path, or resolved some other way.

EDIT: It appears most of the people don't bother reading the linked blog and the original extract did not quote the full solution, so I've expanded the quote from the blog below.

There are numerous ways to resolve the error, based on the likes to all other answers. One known to work is quoted below. Scan though other answers they may be more appropriate for you.

When I restarted my PowerShell prompt, it told me it could not start SSH Agent.

It turns out that it was not able to find the “ssh-agent.exe” executable. That file is located in C:\Program Files (x86)\Git\bin. but that folder isn’t automatically added to your PATH by msysgit.

If you don’t want to add this path to your system PATH, you can update your PowerShell profile script so it only applies to your PowerShell session. Here’s the change I made.

$env:path += ";" + (Get-Item "Env:ProgramFiles(x86)").Value + "\Git\bin"

On my machine that script is at: C:\Users\Haacked\Documents\WindowsPowerShell\Microsoft.Powershell_profile.ps1

Community
  • 1
  • 1
Taras Alenin
  • 8,094
  • 4
  • 40
  • 32
  • 2
    This should really be the answer! – caveman_dick Aug 14 '12 at 14:18
  • This is the right answer. The accepted answer assumes that TortoiseGit or TortoiseHg is installed. As this answer points out, all you need to fix the problem is Msysgit. – Carl Nov 20 '12 at 21:21
  • I've tried this and I continue to get the same error message... is there anything else I can try to get SSH working in PowerShell? – arb Jul 04 '13 at 18:15
  • @Zero21xxx Not to sound condescending, but just as a sanity check can you run `ssh-agent.exe` from anywhere (just open a command prompt and try running it). If you can't that would mean that you PATH changes did not take hold. Otherwise if you find a solution please amend the answer to help the next person. Cheers! – Taras Alenin Jul 04 '13 at 23:35
  • I can indeed run ssh-agent.exe from the command line (PowerShell)... Any other suggestions? I'm about to migrate this to a complete question. – arb Jul 05 '13 at 01:28
  • 3
    Ugggh adding this directory to PATH made me uncomfortable.. it has all sorts of .exes with generic names conflicting with Windows' commands. Hope it doesn't bite me in the ass someday. – Samuel Katz Jul 29 '13 at 18:40
  • 4
    @SalmanPK: it not only makes me uncomfortable, it is also explicitly discouraged by Git maintainers. And in my case, it broke the PowerShell command line. – skolima Aug 19 '13 at 10:26
  • Check out the other answers listed to see how to do this without mucking up your path (as that is unadvisable according to git, other commenters and the world in general), I like @rohancragg's solution personally – Toby Jan 14 '15 at 12:21
  • @Toby, the link in the answer is the same as rohancragg provided and you refer to as well. The power shell script you refer to is actually 'mucking up your' path as you put it. – Taras Alenin Jan 15 '15 at 00:34
  • @BigT I understood the linked solution to only be affecting the PS environment variables, is that not correct? What I mean is, outside of PS, the change will have had no effect, thus avoiding the issues alluded to by SalmanPK when using the normal cmd prompt, etc – Toby Jan 15 '15 at 09:55
  • @Toby, yes you are correct, it affects only PS session(s) used with the profile you've updated. However for those sessions the PATH is changed. SalmanPK did not specify in what context the PATH was changed, but if he/she meant the system PATH, I would be nervous too. All that said, current official windows git installer will update system PATH for you if you choose "Use Git from the Windows Command Prompt" option, so it can't be all that bad. – Taras Alenin Jan 15 '15 at 23:02
  • 10
    For me, `ssh-agent.exe` was actually located in the `\Git\usr\bin` folder. – Ben Wilde Aug 24 '15 at 21:03
53

You can add two lines to the top of your profile (type ise $profile) to fix this PoshGit issue without polluting your Path. Then run . $profile to reload your profile.

For 32-bit git

Set-Alias ssh-agent "${env:ProgramFiles(x86)}\git\bin\ssh-agent.exe"
Set-Alias ssh-add "${env:ProgramFiles(x86)}\git\bin\ssh-add.exe"

For 64-bit git

Set-Alias ssh-agent "$env:ProgramFiles\git\usr\bin\ssh-agent.exe"
Set-Alias ssh-add "$env:ProgramFiles\git\usr\bin\ssh-add.exe"
brianary
  • 8,996
  • 2
  • 35
  • 29
17

If you are using posh-git and getting this warning, you can turn it off by commenting (adding a '#' sign to) the following line

Start-SshAgent -Quiet

In the file

Documents\WindowsPowerShell\Modules\posh-git\profile.example.ps1
Ahmed
  • 636
  • 1
  • 14
  • 31
  • 5
    As an added bonus, if you don't need/want SSH support, and you're having a really slow powershell launch even with adding ssh-agent.exe PATH fix, commenting this line out speeds the launch up a ton. – MechEthan Mar 14 '13 at 01:05
17

I figured it out. You need to set GIT_SSH environment variable on your machine to point to plink.exe. This could be the one installed with TortoiseHg / TortoiseGit / Git Extensions or you can download it from Putty Download Page, but just make sure that you use the same one system wide. Best to log out from your Windows session to make sure this variable gets set for all Explorer instances and command windows.

skolima
  • 31,963
  • 27
  • 115
  • 151
Khalid Abuhakmeh
  • 10,709
  • 10
  • 52
  • 75
  • After 45 wasted minutes, _this is the only thing that fixed my issues with Git in Powershell not using Pageant as its SSH agent_. I simply created a User Environment Variable for `GIT_SSH` with a value of `C:\Program Files (x86)\PuTTY\plink.exe`. Thanks. – Phil Ricketts Sep 28 '16 at 15:29
3

To run ssh-agent with specified key I wrote this code:

$gitexepath = cmd /c where git
$gitbindir = Join-Path $gitexepath "..\..\bin"
$sshagentpath = Join-Path $gitbindir "ssh-agent.exe"
$sshaddpath = Join-Path $gitbindir "ssh-add.exe"
$keypath = "...key path...":
$sshagentres = cmd /c $sshagentpath 
$env:SSH_AUTH_SOCK = [System.Text.RegularExpressions.Regex]::Match($sshagentres, "(?<=SSH_AUTH_SOCK=).+?(?=;)").Value
$env:SSH_AGENT_PID = [System.Text.RegularExpressions.Regex]::Match($sshagentres, "(?<=SSH_AGENT_PID=).+?(?=;)").Value
cmd /c $sshaddpath $keypath
Stanislav Berkov
  • 5,929
  • 2
  • 30
  • 36
2

This has been fixed; see https://github.com/dahlbyk/posh-git/issues/160

You can update posh-git.

Community
  • 1
  • 1
TrueWill
  • 25,132
  • 10
  • 101
  • 150