使用 Visual Studio 2017 通过 SSH 连接到 Git 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42773636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:52:22  来源:igfitidea点击:

Connect to Git repository with SSH using Visual Studio 2017

gitsshvisual-studio-2017

提问by Trisibo

I'm trying to connect to a Git repository (on Bitbucket) with SSH from Visual Studio 2017 (which, as far as I know, supports SSH for Git). I have everything set up, the repository cloned on my computer, and I can commit, but if I try to do something like fetching it fails with the following message (from Visual Studio's "Output" window):

我正在尝试使用来自 Visual Studio 2017 的 SSH 连接到 Git 存储库(在 Bitbucket 上)(据我所知,它支持 Git 的 SSH)。我已经设置好一切,在我的计算机上克隆了存储库,我可以提交,但是如果我尝试执行诸如获取它之类的操作,则会失败并显示以下消息(来自 Visual Studio 的“输出”窗口):

Error encountered while fetching: Git failed with a fatal error.
fatal: Could not read from remote repository.

Trying it from the command prompt, I get these slightly more informative messages:

从命令提示符尝试它,我得到这些信息稍微多一些的消息:

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

From Git Bash I have tried opening the SSH agent, adding my private key, and fetching, and it seems to work (or at least I don't get any messages, unlike when the agent is not started or the key not added):

在 Git Bash 中,我尝试打开 SSH 代理,添加我的私钥,然后获取,它似乎有效(或者至少我没有收到任何消息,这与未启动代理或未添加密钥时不同):

eval `ssh-agent`
ssh-add ~/.ssh/xxxx
git fetch

But Visual Studio is still unable to connect. I have also tried to do the same from the Windows command prompt:

但是 Visual Studio 仍然无法连接。我还尝试从 Windows 命令提示符执行相同操作:

ssh-agent
set SSH_AUTH_SOCK=/tmp/ssh-SIAryCa61iz9/agent.11128
set SSH_AGENT_PID=9804
ssh-add xxxx
git fetch

But I still get the same error.

但我仍然得到同样的错误。

I already added the public key to Bitbucket, and ssh -T [email protected]does output "logged in as xxxx". Also, I can connect correctly using SourceTree and adding the private key to Pageant (the key I use for ssh-addhas the required OpenSSH format, I created it from the .ppk one).

我已经将公钥添加到 Bitbucket,并ssh -T [email protected]输出“以 xxxx 身份登录”。此外,我可以使用 SourceTree 正确连接并将私钥添加到 Pageant(我使用的密钥ssh-add具有所需的 OpenSSH 格式,我是从 .ppk 格式创建的)。

回答by Trisibo

I finally managed to make it work, using PuTTY's Pageant authentication agent instead of ssh-agent, and following the steps mentioned here(it's for Visual Studio Code, but works for Visual Studio 2017 and I guess it should work for any application that uses the "official" Git for Windows).

我终于设法让它工作,使用 PuTTY 的 Pageant 身份验证代理而不是ssh-agent,并按照这里提到的步骤(它适用于 Visual Studio Code,但适用于 Visual Studio 2017,我想它应该适用于任何使用“官方”的应用程序适用于 Windows 的 Git)。

Since I already had Pageant installed and a .ppk private key created, I only had to make Git use Pageant, by creating the GIT_SSHWindows environment variable and setting it to the path of the "plink.exe" file (inside the PuTTY installation, for example C:\Program Files (x86)\PuTTY\plink.exe). With that done, I just need to open Pageant and add the private key (and leave it open while working with the repository), and Visual Studio will be able to connect and issue commands just fine.

由于我已经安装了 Pageant 并创建了 .ppk 私钥,因此我只需要通过创建GIT_SSHWindows 环境变量并将其设置为“plink.exe”文件的路径(在 PuTTY 安装中,对于例子C:\Program Files (x86)\PuTTY\plink.exe)。完成后,我只需要打开 Pageant 并添加私钥(并在使用存储库时将其保持打开状态),Visual Studio 将能够正常连接并发出命令。

回答by Raven

there is another way, works for me.

还有另一种方法,对我有用。

  1. connect to Git repository use another ssh client, like ssh.exe. accept the connection. it will generate known_hosts file.
  2. copy known_hostsand id_rsafile into C:\Users\[UserName]\.ssh\
  3. Done. even without start-ssh-agent.
  1. 使用另一个 ssh 客户端连接到 Git 存储库,例如 ssh.exe。接受连接。它将生成 known_hosts 文件。
  2. known_hostsid_rsa文件复制到 C:\Users\[UserName]\.ssh\
  3. 完毕。即使没有 start-ssh-agent。

seems VS2017 run ssh connection on its own, so it ignores key that ssh-add added, and use default path's key only

似乎 VS2017 自己运行 ssh 连接,所以它忽略了 ssh-add 添加的密钥,只使用默认路径的密钥

回答by Sergey Nudnov

Here is a solution which would allow to connect Visual Studio (and Git) to multiple repositories by SSH, with the separate private keys if necessary and ssh authentication agent to handle the keys' passphrases.

这是一个解决方案,它允许通过 SSH 将 Visual Studio(和 Git)连接到多个存储库,必要时使用单独的私钥和 ssh 身份验证代理来处理密钥的密码。

It is good for the corporate users, because you don'tneed administrator rights on your computer to follow the steps below.

这对企业用户很有好处,因为您不需要计算机的管理员权限来执行以下步骤。

It is explained on example of Bitbucket, but can be extended to Github and anything else.

它在 Bitbucket 的示例中进行了解释,但可以扩展到 Github 和其他任何东西。

Prerequisites

先决条件

  • Installed Visual Studio
  • Bitbucket account
  • 已安装的 Visual Studio
  • 比特桶账户

Download and install Git

下载并安装 Git

On the opening of a project, Visual Studio will ask you to download and install Git package. You could do so using a link provided in a Visual Studio notification, or using this link.

在打开一个项目时,Visual Studio 会要求您下载并安装 Git 包。您可以使用 Visual Studio 通知中提供的链接或使用此链接来执行此操作。

Install Git for the current user only. Use installation options by default.

仅为当前用户安装 Git。默认情况下使用安装选项。

Generate your private/public keys pair

生成您的私钥/公钥对

  1. Locate a Git folder with ssh-keygen.exe application. By default it is: "%LOCALAPPDATA%\Programs\Git\usr\bin\"for example: "C:\Users\NSM\AppData\Local\Programs\Git\usr\bin\"
  2. Open Command Prompt and go into the folder you found:

    cd "%LOCALAPPDATA%\Programs\Git\usr\bin\"
    
  3. Create a folder to store your keys if it doesn't exist

    mkdir "%HOME%\.ssh"
    
  4. Generate a new ssh key:

    ssh-keygen -t rsa -b 4096 -C "<your email of id>" -f "%HOME%/.ssh/id_rsa_<file name>"
    

    for example:

    ssh-keygen -t rsa -b 4096 -C "nsm" -f "%HOME%/.ssh/id_rsa_nsm"
    

    It is advisable to specify they key's passphrase. Remember the passphrase, you wont be able to recover it if forgotten!

  1. 找到带有 ssh-keygen.exe 应用程序的 Git 文件夹。默认为: “%LOCALAPPDATA%\Programs\Git\usr\bin\”例如: “C:\Users\NSM\AppData\Local\Programs\Git\usr\bin\”
  2. 打开命令提示符并进入您找到的文件夹:

    cd "%LOCALAPPDATA%\Programs\Git\usr\bin\"
    
  3. 创建一个文件夹来存储您的密钥(如果它不存在)

    mkdir "%HOME%\.ssh"
    
  4. 生成新的 ssh 密钥:

    ssh-keygen -t rsa -b 4096 -C "<your email of id>" -f "%HOME%/.ssh/id_rsa_<file name>"
    

    例如:

    ssh-keygen -t rsa -b 4096 -C "nsm" -f "%HOME%/.ssh/id_rsa_nsm"
    

    建议指定他们的密钥密码。记住密码,忘记了就找不回来了!

Add your public key to Bitbucket

将您的公钥添加到 Bitbucket

  1. Open your Bitbucket account management page
  2. Open the SSH keyssection and click Add key
  3. Copy and paste content of the generated public key from the "%HOME%/.ssh/"folder. For example: id_rsa_nsm.pub: ssh-rsa AAAAB3Nza<skipped>BkPqxFQ== nsm
  4. Click Add keybutton to submit your public key
  1. 打开您的 Bitbucket 账户管理页面
  2. 打开SSH 密钥部分,然后单击添加密钥
  3. “%HOME%/.ssh/”文件夹复制并粘贴生成的公钥的内容。例如:id_rsa_nsm.pubssh-rsa AAAAB3Nza<skipped>BkPqxFQ== nsm
  4. 单击添加密钥按钮提交您的公钥

Configure ssh to use your key for Bitbucket source code requests

配置 ssh 以将您的密钥用于 Bitbucket 源代码请求

Create configfile in the "%HOME%/.ssh/"folder with the following content:

“%HOME%/.ssh/”文件夹中创建配置文件,内容如下:

    AddKeysToAgent yes

    Host <Bitbucket FQDN or any label>
      HostName <Bitbucket FQDN>
      User git
      IdentityFile ~/.ssh/id_rsa_<file name>

For example:

例如:

    cd %HOME%/.ssh/
    type config

Output:

输出:

    AddKeysToAgent yes

    Host bitbucket.org
      HostName bitbucket.org
      User git
      IdentityFile ~/.ssh/id_rsa_nsm

AddKeysToAgent yesoption will add the configured private keys to the ssh authentication agent on demand

AddKeysToAgent yes选项将根据需要将配置的私钥添加到 ssh 身份验证代理

Configure Git to use ssh authentication agent

配置 Git 使用 ssh 身份验证代理

Typing the passphrase every time the private key has been used is a tedious burden. To avoid that we will use the ssh authentication agent

每次使用私钥时都要输入密码是一种乏味的负担。为了避免这种情况,我们将使用 ssh 身份验证代理

  1. In the folder, where the ssh-keygen utility was found (by default it is: "%LOCALAPPDATA%\Programs\Git\usr\bin\"), create ssh.cmdfile with the following content:

    @echo off
    setlocal enabledelayedexpansion
    
    ::: File storing SSH_AUTH_SOCK and SSH_AGENT_PID of the running agent
    set __ssh_agent=%HOME%/.ssh/agent.env
    
    if exist %__ssh_agent% goto loadenv
    
    :startagent
    echo Starting SSH Authentication Agent...
    ssh-agent > %__ssh_agent%
    
    :loadenv
    ::: Loading the agent environment variables from 2 first lines of agent.env:
    ::: SSH_AUTH_SOCK and SSH_AGENT_PID
    set /a __count=0
    for /f "tokens=1 delims=;" %%a in (%__ssh_agent%) do (
      if !__count! LSS 2 (
        set %%a
        set /a __count+=1
      ) else (
        goto endloadenv
      )
    )
    :endloadenv
    
    ::: Checking the agent is running
    for /f "tokens=1 delims=, usebackq" %%a in (`TASKLIST /FI "PID eq %SSH_AGENT_PID%" /FO CSV /NH`) do (
      if not %%a == "ssh-agent.exe" goto startagent
    )
    
    :startssh
    ::: Run ssh, passing to it all command line parameters
    ssh.exe %*
    

    This script will load the ssh authentication agent before ssh.exe is executed and allow ssh.exe to communicate with it through socket specified in the SSH_AUTH_SOCK environment variable.

  2. From the Command Prompt execute the following command:

    git config --global core.sshCommand '%LOCALAPPDATA%/Programs/Git/usr/bin/ssh.cmd'
    
  1. 在找到 ssh-keygen 实用程序的文件夹中(默认为: "%LOCALAPPDATA%\Programs\Git\usr\bin\"),创建包含以下内容的ssh.cmd文件:

    @echo off
    setlocal enabledelayedexpansion
    
    ::: File storing SSH_AUTH_SOCK and SSH_AGENT_PID of the running agent
    set __ssh_agent=%HOME%/.ssh/agent.env
    
    if exist %__ssh_agent% goto loadenv
    
    :startagent
    echo Starting SSH Authentication Agent...
    ssh-agent > %__ssh_agent%
    
    :loadenv
    ::: Loading the agent environment variables from 2 first lines of agent.env:
    ::: SSH_AUTH_SOCK and SSH_AGENT_PID
    set /a __count=0
    for /f "tokens=1 delims=;" %%a in (%__ssh_agent%) do (
      if !__count! LSS 2 (
        set %%a
        set /a __count+=1
      ) else (
        goto endloadenv
      )
    )
    :endloadenv
    
    ::: Checking the agent is running
    for /f "tokens=1 delims=, usebackq" %%a in (`TASKLIST /FI "PID eq %SSH_AGENT_PID%" /FO CSV /NH`) do (
      if not %%a == "ssh-agent.exe" goto startagent
    )
    
    :startssh
    ::: Run ssh, passing to it all command line parameters
    ssh.exe %*
    

    此脚本将在执行 ssh.exe 之前加载 ssh 身份验证代理,并允许 ssh.exe 通过 SSH_AUTH_SOCK 环境变量中指定的套接字与其通信。

  2. 从命令提示符执行以下命令:

    git config --global core.sshCommand '%LOCALAPPDATA%/Programs/Git/usr/bin/ssh.cmd'
    

Create a remote for your local repository using the Bitbucket provided link

使用 Bitbucket 提供的链接为您的本地存储库创建远程

Replace FQDN in the link with a label you provided in the ./ssh/configfile. That will allow you to use config sections with the multiple keys when connecting to Bitbucket or Github repositories

将链接中的 FQDN 替换为您在./ssh/config文件中提供的标签。这将允许您在连接到 Bitbucket 或 Github 存储库时使用具有多个键的配置部分

All Done

全部完成

回答by Rahul Shanbhag

Well, I couldn't find the direct way to configure Git repository with VS 2017 as the Clone option accepts only the URI not the SSH.

好吧,我找不到使用 VS 2017 配置 Git 存储库的直接方法,因为克隆选项只接受 URI 而不是 SSH。

I did the following:

我做了以下事情:

  1. Generated SSH key using Git Bash and synced the Git server with the keys generated by Git bash. You should be giving the public key under Settings.
  2. Downloaded Sourcetree client and configured it with SSH key Options->General->SSH Client Configuration -> Need to give the private key generated by Git bash
  3. Clone the client using Source tree and Open the same solution from Visual Studio. Push/Pull will work as usual.
  1. 使用 Git Bash 生成 SSH 密钥并将 Git 服务器与 Git bash 生成的密钥同步。您应该在“设置”下提供公钥。
  2. 下载Sourcetree客户端并配置SSH密钥Options->General->SSH Client Configuration->需要给出Git bash生成的私钥
  3. 使用源树克隆客户端并从 Visual Studio 打开相同的解决方案。推/拉将照常工作。

Using Git Bash to generate the Keys you may find here.

使用 Git Bash 生成您可以在此处找到的密钥。

回答by ndberg

This way worked for me (VS 2017 / Win7):

这种方式对我有用(VS 2017 / Win7):

  1. I installed git-scmand connected with it.
  2. Done (nothing to copy, it just works)
  1. 我安装了git-scm并与之连接。
  2. 完成(没有什么可复制的,它只是有效)

回答by ahmeticat

In VS2017

在 VS2017

I had same problem.i tried a few method for solution :

我有同样的问题。我尝试了几种解决方法:

  1. Removed "git for windows" on Visual Studio Installer and delete git on control panel
  2. Installed two of them.
  3. tried some ssh settings
  1. 在 Visual Studio 安装程序上删除了“git for windows”并在控制面板上删除了 git
  2. 安装了其中的两个。
  3. 尝试了一些 ssh 设置

but not solved my problem. And i opened "Visual Studio Installer"and repaired.it was solved.

但没有解决我的问题。我打开“Visual Studio Installer”修复。它解决了。