"Permission denied" error from Visual Studio Code and Visual Studio (but not Git Bash)
Asked Answered
W

7

13

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.

Weixel answered 18/11, 2020 at 14:52 Comment(3)
This was already asked here. Not answered yet, might still be relevant.Lyckman
This was the resolution, not the answer given. I needed a C:\Users\<USER_NAME>\.ssh\config file which named the SSH key files.Weixel
If you're using ssh, see https://mcmap.net/q/131224/-vs-code-bitbucket-ssh-permission-denied-publickey for a detailed explanation of what's going on, which may help youLoomis
W
8

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

Weixel answered 19/11, 2020 at 11:21 Comment(1)
I have config file with proper values and this still doesn't work. I love GitGraph ext from VSC but sadly can only use with repos with keys with no passphrase. Not sure why they make this so difficult.Amylopsin
N
3

I had the same error and same behavior (from git bash -> OK but from vscode -> KO). It is working with a ssh key without a passphrase but if you really want to use a key with a passphrase (much more secure), you must:

  • Add to your config file in "%HOMEPATH%.ssh\config" the following line:

    ForwardAgent yes

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

  • Have a ssh-agent running and your ssh private key loaded

Note : If you push from a Linux, add to your bash_profile the following code to have an ssh agent running when opening a session:

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

then, load your ssh-key with:

ssh-add ~/.ssh/id_rsa

For Windows, see the AlainD answer.

Ninon answered 7/8, 2021 at 17:50 Comment(2)
Creating SSH key without passphrase is the only working solution for me. To do so, create your SSH key like usual, but at the prompt to enter a passphrase, just press "Enter" 2 times.Peculiarize
I'm sorry but it is not the only solution working. I got it to work with the explanations I gave above. We are several people at my work who use this solution and get to push there code to Gitlab from vscode with a ssh key with a passphrase. Unless it is a personal network (mabe for test for example), I really don't recommend using an ssh key without a passphrase.Ninon
A
2

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

Aludel answered 27/2, 2021 at 11:30 Comment(1)
Changing protocol from ssh to https was it for me too: git remote set-url origin https://github.com/username/ProjectName.gitVincennes
L
1

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.

Lyckman answered 18/11, 2020 at 16:16 Comment(1)
I already had SSH keys, but your answer triggered a deep memory of a limitation in VS Studio, VS Code and Git Gui. If the %HOMEPATH%\.ssh keys are not named exactly id_rsa and id_rsa.pub, then all of those named applications fail security checks. I'll either need to rename my SSH keys or find a way to configure the names of the private/public SSH key files somewhere.Weixel
C
0

I just had a similar issue more or less a few details:

  • On my side I could use git locally, but not fetch anything from the remote source with the same error message. (I was trying to fetch data from the IDE VScode)

Turns out that running the same action with a command line (git fetch) triggered a pop-up asking me to re-authenticate with my SSH key's passphrase.

After re-authentication, I could use VSCode's fetch action (and other actions related to the remote sources).

In other words: for anybody facing the same error message, it's worth to check if you don't need to re-authenticate with your SSH key (if you have defined one and you are communicating with git through SSH), or your credentials (if you are communicating through https)

  • You can check if you're using SSH or HTTPS by running the command line: git remote -v
  • You can change from one to the other by running the command: git remote set-url origin [email protected]:OWNER/REPOSITORY.git

Here is the official documentation: https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories#switching-remote-urls-from-https-to-ssh

Clomp answered 26/1, 2024 at 16:30 Comment(0)
V
0

I'm moved onto new computer, copied public key to ssh folder from Gitlab(in-house) UI and got an error from question above.

Fixed that with details below.
I forgot that i should move to .shh folder from old computer also a private key(named id_rsa) to pair it with my gitlab ssh public key id_rsa.pub.

BTW: I know this is stupid mistake, but i hope this will help someone like me, very small percent of the people XD.

Vitalize answered 9/7, 2024 at 13:49 Comment(0)
E
0

I got the same problem and the reason was that I haven't made any SSH keys. Here's how I fixed it:

  • Open git bash or the Mac Terminal.
  • Type cd ~/.ssh. This will take you to the root directory for Git
  • Within the .ssh folder, there should be these two files: id_rsa and id_rsa.pub. These are the files that tell your system how to communicate with GitHub, gitlab, or any other Git based service. Type ls to see a directory listing. If those two files don't show up, proceed to the next step. NOTE: Your SSH keys must be named id_rsa and id_rsa.pub in order for Git, GitHub, and gitlab to recognise them by default.
  • To create the SSH keys, type ssh-keygen -t rsa -C "[email protected]". This will create both id_rsa and id_rsa.pub files.
  • Now, go and open id_rsa.pub in your favourite text editor.
  • Copy the contents "exactly as it appears, with no extra spaces or lines" of id_rsa.pub and paste it into GitHub and/or BitBucket under the Account Settings > SSH Keys.
  • Now that you've added your public key to Github and/or BitBucket, try to git push again and see if it works. It should!

More help available here

Eider answered 9/7, 2024 at 14:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.