Reputation: 1123
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
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
Reputation: 345
there is another way, works for me.
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
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.
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.
Open Command Prompt and go into the folder you found:
cd "%LOCALAPPDATA%\Programs\Git\usr\bin\"
Create a folder to store your keys if it doesn't exist
mkdir "%HOME%\.ssh"
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!
ssh-rsa AAAAB3Nza<skipped>BkPqxFQ== nsm
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
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
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.
From the Command Prompt execute the following command:
git config --global core.sshCommand '%LOCALAPPDATA%/Programs/Git/usr/bin/ssh.cmd'
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
Upvotes: 7
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:
Using Git Bash to generate the Keys you may find here.
Upvotes: 1
Reputation: 1939
In VS2017
I had same problem.i tried a few method for solution :
but not solved my problem. And i opened "Visual Studio Installer" and repaired.it was solved.
Upvotes: 0
Reputation: 3971
This way worked for me (VS 2017 / Win7):
Upvotes: 0
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