Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 626
Trisibo Avatar asked Mar 13 '17 21:03

Trisibo


People also ask

How do I clone a Git repository in Visual Studio using SSH?

Visit your repository on the web and select the Clone button in the upper right. Select SSH and copy the new SSH URL. In your Git client, run: git remote set-url <remote name, e.g. origin> <new SSH URL> . Alternatively, in Visual Studio, go to Repository Settings, and edit your remotes.


2 Answers

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.

like image 68
Trisibo Avatar answered Sep 18 '22 19:09

Trisibo


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

like image 23
Raven Avatar answered Sep 18 '22 19:09

Raven