Trisibo
Trisibo

Reputation: 1123

Connect to Git repository with SSH using Visual Studio 2017

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):

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):

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:

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-add has the required OpenSSH format, I created it from the .ppk one).

Upvotes: 34

Views: 86146

Answers (7)

daboss
daboss

Reputation: 41

For me (Visual Studio 2019), it worked after adding a new system-wide environment variable GIT_SSH with the value C:\Program Files\TortoiseGit\bin\TortoiseGitPlink.exe (TortoiseGit's plink) TortoiseGitPlink.exe). Putty's plink.exe did not do the trick for me. Maybe because I am using TortoiseGit. After adding this, remember to restart Visual Studio.

Upvotes: 4

Raven
Raven

Reputation: 345

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_hosts and id_rsa file into C:\Users\[UserName]\.ssh\
  3. Done. even without 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

Upvotes: 13

Sergey Nudnov
Sergey Nudnov

Reputation: 1429

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.

It is good for the corporate users, because you don't need 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.

Prerequisites

  • Installed Visual Studio
  • Bitbucket account

Download and install 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.

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

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!

Add your public key to Bitbucket

  1. Open your Bitbucket account management page
  2. Open the SSH keys section 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 key button to submit your public key

Configure ssh to use your key for Bitbucket source code requests

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

    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 yes option will add the configured private keys to the ssh authentication agent on demand

Configure Git to use ssh authentication agent

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

  1. In the folder, where the ssh-keygen utility was found (by default it is: "%LOCALAPPDATA%\Programs\Git\usr\bin\"), create ssh.cmd file 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'
    

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

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

All Done

Upvotes: 7

Rahul Shanbhag
Rahul Shanbhag

Reputation: 31

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.

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.

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

Upvotes: 1

ahmeticat
ahmeticat

Reputation: 1939

In 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

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

Upvotes: 0

ndberg
ndberg

Reputation: 3971

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

  1. I installed git-scm and connected with it.
  2. Done (nothing to copy, it just works)

Upvotes: 0

Trisibo
Trisibo

Reputation: 1123

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).

Since I already had Pageant installed and a .ppk private key created, I only had to make Git use Pageant, by creating the GIT_SSH Windows 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.

Upvotes: 46

Related Questions