31

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.

Community
  • 1
  • 1
Irrationnelle
  • 492
  • 1
  • 8
  • 13

5 Answers5

69

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"

mushcraft
  • 1,994
  • 1
  • 19
  • 27
  • 3
    Finally, this is exactly what I was looking for. – reaper_unique Dec 21 '16 at 08:47
  • 8
    Just to add, you can also create a window shortcut and in the target you can put `cmd /c start-ssh-agent & code` – Joshua H Jul 16 '17 at 18:31
  • Strangely, this causes studio code to open twice for me - but at least git push works! – Clayton Jun 15 '18 at 05:02
  • 1
    @Clayton dirty files? https://github.com/Microsoft/vscode/issues/16820 https://stackoverflow.com/questions/39890926/visual-studio-code-prevent-same-file-opening-twice – mushcraft Jun 15 '18 at 10:16
  • so from now on I will need to do this in order to push something in a SSH repo? – dawn Sep 13 '18 at 02:44
  • @dawn well you can add all this in your ~/.bash_profile and make it a set of start up commands. on windows I had to start the ssh-agent before vscode. But I had wincred remeber my ssh credentials so I don't have to type my password all the time. – mushcraft Sep 13 '18 at 09:52
  • " Seems VS Code uses 'id_rsa.pub' key only" was the thing I was missing. That seems like an important piece of information I missed somewhere in the docs. – Semicolons and Duct Tape May 14 '20 at 13:25
5

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
StarLord27
  • 111
  • 3
  • 7
3

My Bitbucket repo's remote was set up to use the
git@bitbucket.org: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://username@bitbucket.org/teamname/reponame.git

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

Tackle
  • 313
  • 2
  • 9
1

if your ssh key need passphrase, then you can try below to skip it, it works fine for me

  1. eval "$(ssh-agent -s)"
  2. ssh-add -K .ssh/id_rsa
  3. add this default ssh configuration works for me

Host * AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_rsa

Note, the 3rd step is very important

neurobot
  • 328
  • 2
  • 9
-1

Your problems is that you probably didnt configured the right ssh keys.


Simply follow those steps and you will set up your ssh key in no time:

  • Generate a new ssh key (or skip this step if you already have a key)
    ssh-keygen -t rsa -C "your@email"

  • Once you have your key set in home/.ssh directory (or Users/<your user>.ssh under windows), open it and copy the content


How to add sh key to github account?

  • Login to github account
  • Click on the rancher on the top right (Settings)
    github account settigns
  • Click on the SSH keys
    ssh key section
  • Click on the Add ssh key
    Add ssh key
  • Paste your key and save

And you all set to go :-)

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167