Fixing Git Permission Denied (Public Key) in Bash Shell [Windows]

ยท

2 min read

Fixing Git Permission Denied (Public Key) in Bash Shell [Windows]

Story

I have just finished a feature by the end of my work day, and am ready to push my pretty codes to the repo and about to enjoy an accomplished moment ๐Ÿš€...

BUT out of no where, after I run git push origin feature/my-awesome-code,
my Bash shell yells at me with this ๐Ÿคฏ:

ssh-error-permission-deny.png

This gets me thinking and checking my SSH key and stuffs... all are correct!
What on earth is wrong!!! ๐Ÿ˜ต
And who cares about Git GUI, because pros only use Git command here. ๐Ÿ˜

After, and after some tiring searches, I finally found the root cause of this problem.

Root Cause

The root cause is that Bash shell does not know my HOME

Normally, in Bash shell if you run cd ~, it will goes to your home directory (e.g. C:\users\username) where you stored your SSH key, instead it shows this weird /y drive which I don't remember existed in my computer.

Group 2.png

and obviously, you list files in this /y, you got nothing, that's why it shows above error permission denied (publicKey), simply means the public key is not found there.

Solution

To solve it, you need to set environment variable HOME to let your Bash shell aware.

  • Window search for "Edit the system environment variables", will lead you to this dialog image.png

  • Set HOME to your home directory where SSH key is stored. image.png

  • OK, Apply, OK...

  • Restart your bash shell for it to take effect

  • You've done, you've solved it! ๐ŸŽ‰

Extra Catches

If it still does not work for you,

  • try to restart your computer after setting the environment variable (very most likely no need to do so, but just do otherwise ๐Ÿ˜‚)
  • Bash shell already got the public key, but you yourself miss configuring it in your remote repository, etc... ๐Ÿ™„
ย