简体   繁体   English

在 Windows 上使用 git 和 ssh-agent

[英]Using git with ssh-agent on Windows

I'm on Windows. I installed git and posh-git (some helpers for Windows PowerShell).我在 Windows。我安装了 git 和 posh-git(Windows PowerShell 的一些助手)。 I can add keys with ssh-add and can authenticate with github and my webserver.我可以使用ssh-add添加密钥,并可以使用 github 和我的网络服务器进行身份验证。 I can also use git from the PowerShell to interact with my repositories.我还可以使用 PowerShell 中的 git 与我的存储库进行交互。

But there is one thing I can't do: I use git-plus for the Atom editor.但是有一件事我不能做:我使用 git-plus 作为 Atom 编辑器。 And I don't get it to push to my repo.而且我无法将其推送到我的回购协议。 What is my problem?我的问题是什么?

posh-git and git for windows 2.7 should include everything you need to setup an ssh-agent . 用于Windows 2.7的 posh-gitgit应该包含设置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: 然后,您应该看到设置了SSH_AUTH_SOCK环境变量:

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. 运行命令时,git-plus atom包应该能够使用哪个。 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). 我能够使用Ctrl + Shift + H调出原子中的git菜单,选择push,然后推送到远程仓库(如果失败则不显示错误,但我推送的新分支就在那里) 。

The ssh-agent needs to be started BEFORE you open atom so that the SSH_AUTH_SOCK environmental variable is set. 在打开atom之前需要启动ssh-agent,以便设置SSH_AUTH_SOCK环境变量。 If it still doesn't work you may want to test ssh in PowerShell to verify that it can connect without a password: 如果它仍然不起作用,您可能希望在PowerShell中测试ssh以验证它可以在没有密码的情况下连接:

Set-Alias ssh "$env:ProgramFiles\git\usr\bin\ssh.exe"
ssh hostname

Since a couple of years, the ssh part has been separated from posh-git , and is now available through posh-sshell .几年以来, ssh 部分已经从posh-git中分离出来,现在可以通过posh-sshell

To install:安装:

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

In your ps1 file:在你的 ps1 文件中:

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

It should automagically pick up any key-files in your ~/.ssh .它应该会自动获取~/.ssh中的任何密钥文件。

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:如果您将Windows 的本机 OpenSSH 实现与本机 ssh-agent Windows 服务一起使用,请确保 git for Windows 在安装时配置为使用该 SSH 实现:

Windows 安装程序的 Git 截图;选择 SSH 可执行文件。选择“使用外部 OpenSSH”选项。

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.如果您使用捆绑的 OpenSSH 安装,git 将默认为该安装,并且不会使用导入到 Windows 本地 ssh-agent 服务中的任何密钥。 You need to select "Use external OpenSSH" instead when prompted.当出现提示时,您需要 select “使用外部 OpenSSH”。

If you did not make this selection when installing, you should be able to fix that by just running the installer again.如果您在安装时没有做出此选择,您应该能够通过再次运行安装程序来修复它。

You can get the ssh-agent running using the command that comes with Git for Windows in powershell: 您可以使用PowerShell中的Git for Windows附带的命令运行ssh-agent:

 start-ssh-agent.cmd

That will start up the ssh-agent. 这将启动ssh-agent。

Then you can add your key with 然后你可以添加你的密钥

 ssh-add ~/.ssh/namneOfPrivateKey

Found that here: https://docs.microsoft.com/en-us/azure/devops/repos/git/use-ssh-keys-to-authenticate?view=azure-devops 在这里找到: https//docs.microsoft.com/en-us/azure/devops/repos/git/use-ssh-keys-to-authenticate?view = azure-devops

The issue is the git was unable to find the ssh_agent.exe which supplied the credentials The steps I followed are given below 问题是git无法找到提供凭据的ssh_agent.exe。下面给出了我遵循的步骤

  1. Searched the ssh_agent.exe file drive 搜索了ssh_agent.exe文件驱动器
  2. Added the path as environment path int the profile.example.ps1 file 在profile.example.ps1文件中添加路径作为环境路径

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM