'"Permission denied" error from Visual Studio Code and Visual Studio (but not Git Bash)

With a new work laptop running Windows 10, I have installed git, Visual Studio Code and Visual Studio 2019. After making some test changes to code in from my personal git repo (hosted on github), I am trying to commit and push those changes to the remote repo.

I can perform all of the Stage, Commit and Push actions from Git Bash. But in Git Gui, VS Code and VS 2019, Stage and Commit both work fine, but Push fails with an error message.

For VS Code the error is:

[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

And for VS 2019 the error is similar:

[email protected]: Permission denied (publickey).
Error encountered while pushing to the remote repository: Git failed with a fatal error.
Could not read from remote repository.

How do I resolve this?

Update

The comment from @DaemonPainter helped lead to the answer. My private SSH key files were named id_rsa_github and id_rsa_github.pub. Visual Studio 2019, Code and Git Gui all expect these to be named id_rsa and id_rsa.pub.

The solution is to add a config file to %HOMEPATH%\.ssh with contents similar to this:

host github.com
 HostName github.com
 IdentityFile ~/.ssh/id_rsa_github

After making that change, git push works in Git Bash, Gut Gui, VS Code and Visual Studio.



Solution 1:[1]

As @DaemonPainter points out, one possibility is that the SSH identity and keys are missing. For that problem, this answer describing how to add your SSH keys may be of interest.

My issue turned out to be the naming of the SSH key files. By default, these are in the %HOMEPATH%\.ssh folder with filenames id_rsa and id_rsa.pub. Developer tools such as Visual Studio 2019, and Git Gui expect that naming convention.

Since my files are for a personal git repo on Github, I had named the SSH key files id_rsa_github/.pub. In %HOMEPATH%\.bashrc, the following lines were added (originally configured on another PC and copied over):

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

That explains why it worked for Git Bash.

To configure non-standard named SSH keys, follow these steps:

  1. Add a file called %HOMEPATH%\.ssh\config

  2. Edit config with contents similar to the following (use # to add comments):

    host github.com
     # My personal git repo for (something-cool)
     HostName github.com
     IdentityFile ~/.ssh/id_rsa_github
    
  3. Restart your development tools, such Visual Studio Code

Solution 2:[2]

I had the same error and same behavior (from git bash OK but from vscode KO). I tried everything I could to resolve this issue. The only thing that worked was to create a new ssh key but this time without a passphrase. And then it worked.

EDIT :

It is working without passphrase but if you really want to use a passphrase, you must :

  • Add to your config file "%HOMEPATH%.ssh\config" :

    ForwardAgent yes

  • Add your public key to your remote repo (Gitlab or Github here)

  • Have a ssh-agent running.

Note : If you push from a Linux for example, add to your bash_profil the following code :

export SSH_AUTH_SOCK=~/.ssh/ssh-agent.$HOSTNAME.sock 
ssh-add -l 2>/dev/null >/dev/null 
if [ $? -ge 2 ]; then 
  ssh-agent -a "$SSH_AUTH_SOCK" >/dev/null 
fi

For Windows, see the AlainD answer.

Solution 3:[3]

Solution inside VS2019 without touching git bash / .ssh files / config

  1. Open VS2019 Menu > Git > Manage Remotes

  2. Click to highlight the "origin" entry with "Fetch & Push"

  3. Edit "origin"

  4. Change "Fetch & Push" from ssh:

    [email protected]:YourRepoFolder/YourRepoName.git

  5. to https:

    https://github.com/YourRepoFolder/YourRepoName.git

  6. Save

  7. OK

  8. Push repo

Solution 4:[4]

Most likely you are trying to push via SSH without a key set (see this question).

Your Git Bash is pushing via HTTP and has no problem of sort. Check your settings in VS.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 grzwolf
Solution 4 Daemon Painter