8

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:

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

And for VS 2019 the error is similar:

git@github.com: 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.

AlainD
  • 5,413
  • 6
  • 45
  • 99
  • 1
    This was already asked [here](https://stackoverflow.com/questions/16245606/could-not-read-from-remote-repository). Not answered yet, might still be relevant. – Daemon Painter Nov 18 '20 at 16:14
  • This was the resolution, not the answer given. I needed a `C:\Users\\.ssh\config` file which named the SSH key files. – AlainD Nov 18 '20 at 18:39
  • If you're using ssh, see https://stackoverflow.com/a/72029153/308451 for a detailed explanation of what's going on, which may help you – JBSnorro May 11 '22 at 11:28

4 Answers4

5

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

AlainD
  • 5,413
  • 6
  • 45
  • 99
  • 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. – liquidcms Apr 17 '22 at 15:52
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.

Pozinux
  • 964
  • 2
  • 10
  • 22
  • 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. – David Jun 14 '22 at 12:26
  • 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. – Pozinux Jun 14 '22 at 21:30
1

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:

    git@github.com:YourRepoFolder/YourRepoName.git

  5. to https:

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

  6. Save

  7. OK

  8. Push repo

grzwolf
  • 11
  • 1
  • 2
  • Changing protocol from ssh to https was it for me too: `git remote set-url origin https://github.com/username/ProjectName.git` – Roger Feb 05 '22 at 16:04
0

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.

Daemon Painter
  • 3,208
  • 3
  • 29
  • 44
  • 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. – AlainD Nov 20 '20 at 12:26