9

Cloning a bitbucket repo using Visual Studio 2019 not working. I am able to clone using command line but not using Visual Studio 2019 Git extension.

Command Line:

start-ssh-agent.cmd

then

git clone <ssh url>

This works fine in command line, but when trying to clone using Visual Studio 2019 (Default Git extension) I am getting this error.

enter image description here

Cloning works fine using gitbash command also.

$ eval ssh-agent -s

$ ssh-add id_rsa

VS git extension works fine for https urls but not using SSH urls.

I created key by following this link https://confluence.atlassian.com/bitbucketserver067/creating-ssh-keys-979426660.html and updated the key in bitbucket.

Any of you have encountered this issue or am I missing any steps here to configure VS 2019 to work for SSH Urls?

Thanks

HashCoder
  • 946
  • 1
  • 13
  • 32

3 Answers3

4

I do not use Bitbucket with Visual Studio 2019, I use GitLab with Visual Studio 2019 via SSH. But I guess the procedure is the same.

I will explain you the structure I use in order to manage SSH keys used for GIT (Github, GitLab etc.). So far it always worked out for me, so maybe it does also for you:)

Search the folder %userprofile%/.ssh on your system and create a folder bitbucket.org in it. If you are not able to find the folder .ssh you can simply create one in %userprofile%. Copy the public and private ssh key into the newly created folder (bitbucket.org). The name of the file should be the same as your user name on bitbucket. The private ssh key should not have any file extension. The public ssh key should end in *.pub.

After having created the folder and copied all keys, you need to create a file config in %userprofile%/.ssh with the following content.

# Bitbucket
Host bitbucket.org
    Hostname bitbucket.org
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/bitbucket.org/{username}

You have to substitute {username} with the correct file name.

Your folder structure should be the following:

  • %userprofile%/.ssh
    • bitbucket.org
      • username
      • username.pub
    • config

You may need to restart Visual Studio 2019 and all terminals.

HTH

zanseb
  • 1,275
  • 10
  • 20
  • 1
    I tested it on my machine with bitbucket and it works. Be careful when using the `clone` button in Bitbucket as you copy also the `git clone` part of the url which shall not be entered in Visual Studio. If you are using a self hosted variant of bitbucket, you need to swap `bitbucket.org` with the actual host (`-bitbucket..com`). – zanseb Feb 03 '20 at 15:08
  • I generated the key using gitbash command line and add the key to ssh agent, After this I tried to clone using Visual Studio and worked. Thanks for you help. – HashCoder Feb 05 '20 at 10:27
1

Check first your environment variables (active when you launch Visual Studio)

If you still have a GIT_SSH referencing plink, remove it (and relaunch VSCode): Recent Git for Windows should use OpenSSH by default.

Then, for testing, set a simplified %PATH%:

set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\
set GH=C:\path\to\git
set PATH=%GH%\bin;%GH%\usr\bin;%GH%\cmd;%GH%\mingw64\bin;%PATH%

Generate a key without a passphrase (again, for testing), in that CMD session, using the old format.

ssh-keygen -m PEM -t rsa -P "" -f afile

If you are using the file by default (no -f parameter), that would be easier.
If not, you can setup a %USERPROFILE%/.ssh/config file as described here.

The OP HashCoder adds in the comments:

I generated the key using gitbash command line and add the key to ssh agent.
After this, I tried to clone using Visual Studio and worked.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I followed the suggestion mentioned in the link and I am getting an error when trying to clone the repo - %PROGRAMFILES'\(X86'\)%/Git/usr/bin/ssh.cmd: line 0: fg: no job control Do you know why this is happening? – HashCoder Jan 31 '20 at 11:49
  • @HashCoder Which link? And what version of Git are you currently using? – VonC Jan 31 '20 at 12:03
  • Git Version is 2.25.0 and the link is https://stackoverflow.com/questions/42773636/connect-to-git-repository-with-ssh-using-visual-studio-2017/53677552#53677552 – HashCoder Jan 31 '20 at 15:31
  • @HashCoder OK. I suppose that link content might need to be changed/adapted to your use case. ssh.cmd is not needed here (no passphrase, so no ssh-agent) – VonC Jan 31 '20 at 16:44
  • I tried the steps you mentioned, which also resulted in the same error mentioned in my question. – HashCoder Feb 03 '20 at 10:09
  • @HashCoder Then the remote repository, as specified in Visual Studio, seems incorrect. – VonC Feb 03 '20 at 10:11
  • Cloning works fine when I use command prompt or Git Bash using the same url but not using Visual Studio. – HashCoder Feb 03 '20 at 13:25
  • @HashCoder Is it an SSH URL like `git@bitbucket.org:/`? – VonC Feb 03 '20 at 13:26
  • url is like ssh://git@-bitbucket..com://.git – HashCoder Feb 03 '20 at 13:34
  • @HashCoder Did you try, for testing, with an https URL (a bit like in https://www.donovanbrown.com/post/Building-From-On-Premises-Bitbucket-Server-Using-Visual-Studio-Team-Services)? – VonC Feb 03 '20 at 14:31
  • @HashCoder I would recommend asking an admin for that BitBucket comanyname on-premise instance to look as the SSH daemon logs (possibly in `/var/log/auth.log`) provided the SSH daemon is launched with `-d` (debug mode, as mentioned in https://stackoverflow.com/a/22740720/6309) – VonC Feb 03 '20 at 16:27
  • I generated the key using gitbash command line and add the key to ssh agent, After this I tried to clone using Visual Studio and worked. Thanks for you help. – HashCoder Feb 05 '20 at 10:25
  • 1
    @HashCoder Great! I have included your comment in the answer for more visibility. – VonC Feb 05 '20 at 10:29
0

Error: Please make sure u have the correct access rights or the remote repository exists.

usually happens on two scenarios.

1) your internet is down(Yes. it happens).

2) it's your ssh configuration.

it's not just generating the key, it's important to add to your bitbucket account. check whether you did that properly. assuming, u did that all properly

ps: every time you generate, the key will be updated so, it's important to update the new key in your bitbucket account as well.

also, there's no seperate terminal for VS Code. if you are using windows it's your regular cmd or the Windows Power Shell, when you're using linux it's your regular shell prompt which is opened via VS Code.

pope_maverick
  • 902
  • 8
  • 15