54

I'm on Windows. I installed git and posh-git (some helpers for Windows PowerShell). I can add keys with ssh-add and can authenticate with github and my webserver. I can also use git from the PowerShell to interact with my repositories.

But there is one thing I can't do: I use git-plus for the Atom editor. And I don't get it to push to my repo. What is my problem?

LuMa
  • 1,673
  • 3
  • 19
  • 41
  • See http://haacked.com/archive/2011/12/19/get-git-for-windows.aspx/ for details about getting the ssh agent to work in posh-git – Greg Bray Jan 06 '16 at 17:46

6 Answers6

53

posh-git and git for windows 2.7 should include everything you need to setup an ssh-agent. Once you have the module installed you can start the agent using something like:

Import-Module ~\Documents\WindowsPowerShell\Modules\posh-git\posh-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"
Start-SshAgent -Quiet

You then should see the SSH_AUTH_SOCK environmental variable is set:

C:\Code\Go\src\bosun.org\cmd\scollector [master]> gci env:SSH_AUTH_SOCK

Name                           Value
----                           -----
SSH_AUTH_SOCK                  /tmp/ssh-6ORcVQvRBZ2e/agent.11668

Which the git-plus atom package should be able to use when you run commands. I was able to use Ctrl+Shift+H to bring up the git menu in atom, select push, and then push to a remote repo (not it doesn't display errors if it fails, but the new branch I pushed was there).

The ssh-agent needs to be started BEFORE you open atom so that the SSH_AUTH_SOCK environmental variable is set. If it still doesn't work you may want to test ssh in PowerShell to verify that it can connect without a password:

Set-Alias ssh "$env:ProgramFiles\git\usr\bin\ssh.exe"
ssh hostname
Greg Bray
  • 14,929
  • 12
  • 80
  • 104
  • 1
    Yes, the SSH_AUTH_SOCK variable is set. Furthermore connecting without an unprotected key worked from within Atom and git-plus. Agent is running, identities are imported. I can establish a connection with ssh, but git-plus won't work with protected keys. – LuMa Jan 06 '16 at 20:08
  • what do you mean by protected keys? – Greg Bray Jan 06 '16 at 23:14
  • Keys which are protected by a passphrase. For example I create a key following the GitHub docs: https://help.github.com/articles/generating-ssh-keys/ Creating key (id_rsa), protecting it with a passphrase and adding it to the ssh agent. Then I add it to GitHub and my webserver. Now I can use PowerShell, cd into my local project and push/pull without entering the key's passphrase. Even using ssh to connect to my webserver works. However, git-plus won't work. Only if I delete the passphrase from my key (using `ssh-keygen -p`) git-plus will do it's job. So why won't git-plus use the ssh-agent? – LuMa Jan 07 '16 at 14:06
  • After trying for over an hour, this is the only thing that worked.. http://haacked.com/archive/2011/12/19/get-git-for-windows.aspx/ – Blake Niemyjski Jul 18 '17 at 18:32
  • 1
    Adding the alias commands to `My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1` did the trick. – fiat May 23 '18 at 23:19
  • I went through these steps and there is not folder `C:\Code\` – 8protons Jun 11 '19 at 21:47
  • There is no longer `Start-SshAgent` in posh-git. – Andrew Savinykh Oct 07 '19 at 23:17
  • @AndrewSavinykh It's in a separate module called `posh-sshell`. – Kohányi Róbert Oct 19 '19 at 15:00
42

You can get the ssh-agent running using the command that comes with Git for Windows in powershell:

 start-ssh-agent.cmd

That will start up the ssh-agent.

Then you can add your key with

 ssh-add ~/.ssh/namneOfPrivateKey

Found that here: https://learn.microsoft.com/en-us/azure/devops/repos/git/use-ssh-keys-to-authenticate?view=azure-devops

Garry Polley
  • 4,253
  • 1
  • 22
  • 29
  • 1
    Running `start-ssh-agent.cmd` will open a CMD shell. You probably need to stay in this shell in order to run `ssh-add`. `start-ssh-agent.cmd` sets the variable `SSH_AUTH_SOCK`, which is required by `ssh-add`, but this variable cannot be accessed outside this CMD shell. Therefore, `ssh-add` needs to run inside this CMD shell. The CMD equivalent to the example in this answer would be `ssh-add "%USERPROFILE%\.ssh\namneOfPrivateKey"`. – GreenRaccoon23 Jan 08 '20 at 21:20
  • 6
    Also, instead of `ssh-add`, you may need to use `C:\Program Files\Git\usr\bin\ssh-add`. `ssh-add` resolves to `C:\Windows\System32\OpenSSH\ssh-add`, but git provides another version at `C:\Program Files\Git\usr\bin\ssh-add`. `start-ssh-agent.cmd` has a bug where it does not recognize that `ssh-agent` is in the `PATH`, so it uses the git version. Since it uses git's `ssh-agent`, you need to use git's `ssh-add` too. Therefore, you need to specify the full path to the git version. This changes the example command to `"C:\Program Files\Git\usr\bin\ssh-add" "%USERPROFILE%\.ssh\namneOfPrivateKey"`. – GreenRaccoon23 Jan 08 '20 at 21:49
  • This didn't work. It started something but I couldn't add the ssh key then. – wordsforthewise Apr 27 '22 at 17:39
  • @ЗеленыйЕнот23 could you please explain ,how exactly should I write command in Windows cmd? I think your answer is that I need, but I can't write this command. Now I use this: `C:\Program Files\Git\usr\bin\ssh-add C:\Users\Me\.ssh\id_rsa` – Monica May 13 '22 at 09:28
20

Since a couple of years, the ssh part has been separated from posh-git, and is now available through posh-sshell.

To install:

PowerShellGet\Install-Module posh-sshell -Scope CurrentUser
PowerShellGet\Install-Module posh-git -Scope CurrentUser

In your ps1 file:

Import-Module posh-git
Import-Module posh-sshell
Start-SshAgent

It should automagically pick up any key-files in your ~/.ssh.

Jonatan Lindén
  • 1,445
  • 1
  • 16
  • 24
  • 1
    This should now be the accepted answer for any posh-git / ssh issues. Wish I could upvote more than once. – ericb Aug 10 '21 at 17:23
11

If you are using Windows' native implementation of OpenSSH with the native ssh-agent Windows service, make sure that git for Windows was configured to use that SSH implementation when you installed it:

Screenshot of Git for Windows installer; choosing the SSH executable. The "Use external OpenSSH" option is selected.

If you used the bundled OpenSSH installation, git will default to that and will not use any keys imported into Windows' native ssh-agent service. You need to select "Use external OpenSSH" instead when prompted.

If you did not make this selection when installing, you should be able to fix that by just running the installer again.

Ajedi32
  • 45,670
  • 22
  • 127
  • 172
3

git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe"

Configure globally to use OpenSSH agent. Add your keys with

ssh-add.exe <path to ssh key>

  • 1
    Might want to mention setting the OpenSSH service to Automatic(delayed) to start with terminal. – Thell Apr 15 '23 at 04:42
0

The issue is the git was unable to find the ssh_agent.exe which supplied the credentials The steps I followed are given below

  1. Searched the ssh_agent.exe file drive
  2. Added the path as environment path int the profile.example.ps1 file

$env:path += ";" + "C:\Program Files\Git\usr\bin"