To use <git push> on Visual Studio Code, but show "Could not read from remote repository."

35,100

Solution 1

I thought the answer by @codewizard was the correct one. Seems VS Code uses 'id_rsa.pub' key only, it was not using my other ssh key pair that git.exe was configured to use.(This key wasn't name id_rsa.) However after generating a new id_rsa key pair, I still got permission denied (publickey).

I Found my answer on this blog entry, seems vs code doesn't have a ssh-agent to interact with?

http://blog.alner.net/archive/2015/08/24/vs_code_with_git_ssh_keys_that_use_passphrases.aspx

The solution on the blog being

  • Open a command prompt.
  • Run "start-ssh-agent" and answer passphrase prompts.
  • Run "code" to launch VS Code from that environment.

I used git-bash
start the ssh agent: eval "$(ssh-agent -s)"
then "code" to launch VS Code

Note: As @JoshuaH points out in the comments, you can create a windows shortcut to simply the steps above. cmd /c start-ssh-agent & code

git fetch started working. However I started to get a openssh passphrase box every x minutes(untimed)

so I then rechecked the key was added again using git-bash
then "ssh-add ~/.ssh/id_rsa"
then git config --global credential.helper wincred

If you want a password prompt every time, then ignore the two previous commands and disable autofetch in VS Code's settings.
"git.autofetch": "true" in VS code settings to "git.autofetch": "false"

Solution 2

My Bitbucket repo's remote was set up to use the
[email protected]:teamname/reponame.git SSH type of url.

With my passphrase enabled SSH key this was a no go. I changed it to the HTTPS version: https://[email protected]/teamname/reponame.git

Worked fine then. No need to work with other environments in Windows 10 etc.

Solution 3

If you want to use the SSH type of URL in Visual Studio Code's Git Graph for example, then it isn't enough to have a generated SSH key pair and the possibility to do push and pull between local repository and origin repository inside your terminal. Because in the terminal you are always asked for your pass-phrase to authenticate your private SSH credentials.

In order to connect to a Github repo over SSH in VSCode, you need to add the SSH key to the SSH agent as well.

These are the additional steps you need to perform (using the standard MacOS version of ssh-add):

  1. Check there are no existing identities add by the SSG Agent: ssh-add -l
  2. create an ~/.ssh/config file, add the following lines:
 Host *
 UseKeychain yes
 AddKeysToAgent yes
 IdentityFile ~/.ssh/id_rsa

and save.

This tells the SSH Agent to automatically load the keys and store the pass-phrases in your MacOS keychain.

  1. add your private key to the SSH Agent: ssh-add -K ~/.ssh/id_rsa
  2. Check if the agent successfully stored the key by listing the existing identities: ssh-add -l
Share:
35,100
Irrationnelle
Author by

Irrationnelle

Hello, world!

Updated on October 05, 2021

Comments

  • Irrationnelle
    Irrationnelle over 2 years

    I started studying Git and GitHub.

    And now, I could create my repository to practice and I could push commits to origin repository(in GitHub) on git bash.

    But when I tried to push on Visual Studio Code, I have received this error

    Permission denied (publickey).

    fatal: Could not read from remote repository.

    and failed to push to origin repository.

    but I already remote local repository to origin repository with ssh key on git bash and I could complete push and pull between local repository and origin repository.

    In others' case, they were asked GitHub credentials to push or sync, but in my case, I could not be asked any credentials like ssh key.

    In this case, What should I do? Thank you.