24

I already updated my Git for Windows. I checked the keys are correct. The only "solution" is to start

ssh-agent

Or opening VSCode via Git Bash.

So, any useful solution?

Log:

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

Please make sure you have the correct access rights
and the repository exists.
dawn
  • 1,327
  • 6
  • 19
  • 39

3 Answers3

24

Force push might be disabled on the master branch. Check the settings on the repo that you're trying to push to.

If force push on master is not disabled, you need to make sure that you have added your ssh key path on your ssh config, so that you don't have to add the ssh key to the session every time. Edit the config file at ~/.ssh/config and add the below (on MacOS)

Host *
  UseKeychain yes
  AddKeysToAgent yes
  IdentityFile ~/path/to/key

For Windows, please check my SO answer to know how to add the ssh key to the ssh config.

UPDATE

Looks like there's an open issue with VS Code on Windows here. You may try the workaround that is mentioned here.

Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54
  • And for Windows? – dawn Sep 13 '18 at 03:32
  • 1
    @dawn added to the answer. Please check my SO answer for the steps to add it on Windows. – Madhu Bhat Sep 13 '18 at 03:34
  • I think I already have it written on that file. I've got it like this already: IdentityFile ~/.ssh/id_rsa # – dawn Sep 13 '18 at 03:36
  • 1
    @dawn Are you sure there is no `#` before `IdentityFile ~/.ssh/id_rsa`? Since the # can comment the line. – Madhu Bhat Sep 13 '18 at 03:37
  • @dawn to make sure that force push is not disabled on the master branch, go to the repo's Settings->Branches->Branch protection rules. Make sure that no rule is enabled on the master branch. But usually you are not supposed to force push to the master branch. – Madhu Bhat Sep 13 '18 at 03:39
  • Okai, I uncommented that line. Also, 'No branch protection rules defined yet.' appears on that site. – dawn Sep 13 '18 at 03:41
  • 1
    @dawn then force push is not disabled. Also, the ssh config should have the local path to your ssh key and not `~/.ssh/id_rsa`. If your ssh key is in D:\ssh_key, you should add the line `IdentityFile D:\ssh_key` to the ssh config. – Madhu Bhat Sep 13 '18 at 03:43
  • Okai. Look, I do can push to the remote repo VIA GIT BASH. Everything works smoothly. The thing is with VSCODE, which it simply can't. Any solution to that? – dawn Sep 13 '18 at 03:45
  • @dawn can you try opening VS Code as an administrator and try pushing? – Madhu Bhat Sep 13 '18 at 03:55
  • Nope, I can't in administrator mode either. The same message appears. But, as I said, I do can via Git Bash. – dawn Sep 13 '18 at 03:57
  • 2
    @dawn looks like there's an open issue with vscode https://github.com/Microsoft/vscode/issues/13680. You may try this workaround that someone has mentioned here https://github.com/Microsoft/vscode/issues/13680#issuecomment-414841885 – Madhu Bhat Sep 13 '18 at 04:05
  • github.com/Microsoft/vscode/issues/13680#issuecomment-414841885 worked for me. – dawn Sep 13 '18 at 04:28
  • Is there a away of confirming that vscode is using the configured key? At the command line you can use `ssh -v git@git-repo-hostname`, this will show which file it used. How would you check this in vscode? (I suppose if you are able to push a change it likely is correct, but sometimes you don't want to push a bogus change just so you can check that). – Oliver Mar 09 '22 at 16:03
4

You could set an remote origin using an access token (in Github) like:
git remote set-url origin https://XXXX-YOUR-ACCESS-TOKEN-XXXXXX@github.com/your-account/your-repo.git

3

There are many ways you can make your ssh key (with a passphrase) works in the gitbash terminal for VS Code. I'll let you look around to find it if you didn't.

I assume you did look around and are still stuck because you want to use the UI of VSCode.

If so, this official page tells you why :

Basically, you'll still have to run it via git bash or via running ssh-agent.

But you can bypass the situation using putty. I didn't try it myself.

Anyway : hope it'll help.

Nicolas HERMET
  • 138
  • 2
  • 10