Search code examples
gitsshpublic-keygit-remote

Why am I getting "permission denied (publickey)" when trying to push to my repository on Github?


I'm on Windows, using Git for Windows 2.40.0.windows.1 I've wanted to use SSH rather than HTTPS to connect to Github, so I've set up an SSH keypair, and added the public key to my Github profile.
I have set up the SSH Agent service and added my private key to it. Get-Service ssh-agent confirms that the agent is running. ssh-add.exe -l confirms that my key is added to the agent. When running ssh -T [email protected] as the SSH key setup suggests, I am successfully authenticated.

The key is not stored at the standard ~/.ssh/id_rsa location and it has a passphrase. I'm not sure if this is relevant, but I think I should add this anyway.

However, after adding the SSH remote URL to my project, it gives me the familiar permission denied (pubickey) whenever I run git push:

PS > git push origin main
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

This issue also happens with Gitlab, assumedly with any SSH connections to git remotes. Notably though, this issue does not happen with git when I use it on a Linux system, so this leads me to believe this is a problem with Git for Windows or my setup specifically.

What can I do here? Is there any configuration I've overlooked? How can I get more information from the git push command? (adding --verbose doesn't really add any more output)


Solution

  • As pointed out by user7860670's comment, Git for Windows comes with its own version of SSH bundled. Thus, whenever I used git commands, it would ignore my SSH agent configuration because I haven't used the bundled SSH agent.

    The solution is to configure Git for Windows to use an external SSH command rather than its bundled one. This can be set during installation or by running the setup file again.