5

Running Windows 8.1 64-bit with the following installed:

  • Git for Bash (v2.24.1.windows.2) using MinGW-W64
  • Visual Studio Code (v1.41.1)

My personal SSH key (located at %USERPROFILE%\.ssh) has been generated and added to my GitHub account. .bashrc located at %USERPROFILE has been modified to add these lines:

# Start ssh-agent to allow git to be used
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa_personal

I start Git Bash (MINGW64) in a git repo. Now make changes to a file, git add, git commit and git push the changes to my git repo...and it works fine!

For the standard Windows command prompt, I've created a file called bashrc.cmd with this code:

rem Start ssh-agent to use git from the Windows command line
set GIT_PATH=C:\Program Files\Git
call "%GIT_PATH%\cmd\start-ssh-agent.cmd"
call "%GIT_PATH%\usr\bin\ssh-add.exe" "%USERPROFILE%\.ssh\id_rsa_personal"

Now, a new REG_SZ value called AutoRun in the key HKLM\SOFTWARE\Microsoft\Command Processor was created which points to the above file (bashrc.cmd)

Start a command prompt, and now ssh-agent is started and my identity is added. As with Git Bash, add, commit and push work perfectly. Awesome!

Now I load Visual Studio Code in my git repo. In the lower left, Code knows I'm inside a git repo:

master branch of my repo

Using the in-built Terminal in Code, I can add, commit and push changes to my git repo. Great!

But now I try it with the user interface buttons. I can stage and commit changes and confirm in a separate command line windows using git status that these work as expected. But when I use the Push command, I get this error:

Permission denied to git repo

Return to the Code Terminal, or a Windows command prompt, or Git Bash, issue the command git push and the changes are sent to the remote repo correctly.

Why is this not working with the Code Push command?

AlainD
  • 5,413
  • 6
  • 45
  • 99
  • I am also using git scm and integrated terminal, I have never tried with icons, but after reading this question, I have tried with my project and I have not faced any difficulties its working good, I have ssh key added in git that's it no more efforts. – Prateik Darji Jan 24 '20 at 14:26
  • Yes, very strange. For now, I use the icons to `Stage` and then `Commit` the changes. Then return to the in-built Terminal and issue the final `git push` command. Why the `Push` command via the user interface is failing remains a mystery. – AlainD Jan 24 '20 at 22:41
  • 1
    @PrateikDarji: Turns out the problem was with the *name* of my SSH key. Even though I explicitly add my personal SSH key, VS Code appears to prefer the default name of `id_rsa`. Probably a Code bug or limitation. See the accepted answer. – AlainD Jan 27 '20 at 12:27

5 Answers5

3

After some efforts I found same error in my Visual Studio Code, even when I tried to use ctrl+shift+p and git:pull command also gives me the same error as describe Permission Denied(publickey)

I read here, that Seems VS Code uses 'id_rsa.pub' key only, and I'm having different name for the key pairs, then I copies private key and public key which was set for the git repository to id_rsa and id_rsa.pub and again I tried to check with icons as well as from menu and it works without adding into ssh-agent because it only search for ~/.ssh/id_rsa at your profile directory. When I deleted id_rsa it gives me the same error and terminal is working as expected with the different key name which is already added in ssh-agent.

Also the thing is, git add . and git commit -m are the commands which works for local not establish connection with remote branch but when you tries to fetch, pull, push tries to connect to remote repository where authentication is required.

Prateik Darji
  • 2,297
  • 1
  • 13
  • 29
  • That could be it! My keys also do not use the default names. Will try later and confirm. – AlainD Jan 27 '20 at 06:44
  • 1
    Thanks! The name of the SSH key was the problem. I had a work-related SSH key called `id_rsa` (with the associated `id_rsa.pub` public key). My personal SSH key was called `id_rsa_personal`. When I (temporarily) renamed the work-related SSH key, then renamed my personal key to `id_rsa`...Visual Studio Code started working. Moreover, I no longer needed to manually start the `ssh-agent`. This feels like a bug in Visual Studio Code. I'll modify the question to include this detail. – AlainD Jan 27 '20 at 12:22
1

The problem also occurs when the key is protected by a passphrase. Regenerating another key without a passphrase solved my problem.

Gachie
  • 11
  • 2
1

I was able to push the changes using iterm on my mac bit not via vs code. So as one of the above user suggested a key without a passphrase might work. I tried it and it did work. I did not have to generate a new key. I used this to update the passphrase. ssh-keygen -p -f /path/to/ssh_key

your ssh key is the one without the .pub extension.

0

After switching my Github account and generate a new SSH key. I was able to git pull from Terminal. But VSCode and IntelliJ could not. It seems to be using my previous credential.

Running eval "$(ssh-agent -s)" in both terminal did the trick for me.

Potatoes
  • 300
  • 3
  • 13
-1

The name of RSA key is the exact reason why it happens. Required key file name has to be ~/.ssh/id_rsa on remote host. Just check it on the latest version.

  • VSCode:1.61.0
  • Remote-SSH: 0.65.8