Fix git ‘Permission Denied (publickey)’ error

When you try to git push it throws Permission Denied(publickey) error.

Permission denied (publickey).
fatal: Could not read from remote repository.

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

When this keeps happening and you think you’ve already added the public key in the GitLab or BitBucket or any other. You might’ve added key in Pageant which comes with putty but git doen’t use the key from there. To solve this open git bash as Administator. (Don’t use windows terminal or default CMD, better use *nix cli)

  • Type cd ~/.ssh It will take you to %userprofile%/.ssh or C:/users/username/.ssh
  • Run ls, there should be two file id_rsa and id_rsa.pub
  • If those files are alredy there, then copy the content of id_rsa.pub file and paste it in the key section of git service provider.
  • if not continue bellow steps.
  • To create SSH key type the following ssh-keygen -t rsa -C “[email protected]
  • replace [email protected] with your email address. If you wish set a keyphrase for the key add it when it prompts.
  • Now do the ls, both files should be created by now. Open the directory and copy the contents of id_rsa.pub file to the key section of your git provides.
  • Now try pushing the git repo with git push, it should work by now.