简体   繁体   中英

git@github.com: 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 git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. git@github.com: 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
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.

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

Generate key pair:

ssh-keygen -t rsa -b 4096 -C “youremail@example.com”

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

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

Fixed it by running this command

ssh -vT git@github.com

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?

On Windows there are sometimes multiple version of SSH installed.
It results in conflict when using git:
git@github.com: Permission denied (publickey) .
To resolve it, just point the correct version in environment variable:

setx /m GIT_SSH C:\Windows\System32\OpenSSH\ssh.exe

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.

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/

The issue for me was that I was on a new PC, trying to access the same account as my other computer. It's not enough to create a new SSH, you also need to register it in your GitHub profile. Once I did, problem solved itself.

I believe the best method copying the public key content from ~./ssh folder is clip command. Since selecting manually and then copying may introduce additional spaces etc.

Here are the steps I would follow.

1- Check to see if you already have a key pair

ls -lah ~/.ssh

in CMD: dir/W %USERPROFILE%\.ssh

2- If you have key pairs, copy the content of the public key to memory

cat ~/.ssh/id_xxxxx.pub | clip

xxxxx could be ed25519 or rsa

in CMD: type %USERPROFILE%\.ssh\id_rsa.pub | clip type %USERPROFILE%\.ssh\id_rsa.pub | clip

3- If you don't have the key pairs, just generate with either one of the following commands:

(first one is preferred).

ssh-keygen -t ed25519 -C "your_email@example.com

or

ssh-keygen -t rsa -b 4096 -C "your_email@example.com

and then do the step 2.

4- Navigate to github and then paste the memory content that you copied with clip

NOTE: Same steps will work with BitBucket, GitLab and Azure repos.

在此处输入图像描述

I had the same issue even after adding ssh keys on github. Previously I was using "CMD" in Windows. Then I changed to "Git Bash" and my issue was resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM