'[email protected]: Permission denied (publickey)

I created a new remote repository and tried to use git push -u origin master command to push my local files into the new repository the first time after I add it and commit it. However, it pops up this [email protected]: Permission denied (publickey). fatal: Could not read from remote repository.. How can I fix this fatal error?

I've tried this
How to solve Permission denied (publickey) error when using Git? It seems that the question in this link happens in the first time using git. I've used my git for a while, do I still need to follow this solution? Can anyone gives me a more specific solution?

This the fatal error that I got

C:\Users\ASUS\Desktop\React-Practice App\my-app>git status
On branch master
nothing to commit, working tree clean

C:\Users\ASUS\Desktop\React-Practice App\my-app>git push -u origin master
[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.


Solution 1:[1]

You’re accessing GitHub through SSH. First generate an SSH key pair; then add the public key to GitHub.

Generate key pair, github prefers the "Ed25519 algorithm"

ssh-keygen -t ed25519 -C "[email protected]"

It still allows using rsa for systems that don't support Ed25519

ssh-keygen -t rsa -b 4096 -C “[email protected]

See more at https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent and https://help.github.com/en/articles/adding-a-new-ssh-key-to-your-github-account

Solution 2:[2]

Had the same issue even after adding ssh keys on github.

Fixed it by running this command

ssh -vT [email protected]

Solution 3:[3]

I had this problem because I used a non-standard location for my key like @Mon did. In my case, I did not use the default file name because I have more than one key on my system. So it's not possible to resolve it simply by moving or renaming.

The solution is to edit or create ~/.ssh/config (OpenSSH docs for config and Configuring the Location of Identity Keys) to contain the following:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-key
  • Do not edit line 3 to reflect your own username. The username should be git (see github docs).

  • Edit line 4 to reflect the location of your key.

If you already have a config file, it would be worthwhile having a look at what's there before futzing around. It never hurts to make a copy before editing.

The above assumes that you have created the key, put it on github, that you actually do have permissions, and everything else is as it should be, but still encountering error.

Solution 4:[4]

When I had this problem, I ended up having a typo in my git remote origin url. Maybe you can double check that everything is spelled correctly without any additional unintended keystrokes?

Solution 5:[5]

I was having the worst time getting this setup and had the same error pop up.

How I fixed my issue was not having the .git folder as root and using another user to access it by using sudo. I changed the folder permission to the user:

chown -R user:user .git/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Eric
Solution 3 stichResist
Solution 4 shrfu31
Solution 5 buddemat