71

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.. 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.
Jessie
  • 1,155
  • 3
  • 16
  • 27
  • Possible duplicate of [GitHub Error Message - Permission denied (publickey)](https://stackoverflow.com/questions/12940626/github-error-message-permission-denied-publickey) – phd Aug 31 '19 at 11:18
  • https://stackoverflow.com/search?q=%5Bgit%5D+Permission+denied+publickey – phd Aug 31 '19 at 11:18

10 Answers10

80

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 "your_email@example.com"

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

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

Community
  • 1
  • 1
Moazzem Hossen
  • 2,276
  • 1
  • 19
  • 30
  • 9
    I've use this command ```ls -al ~/.ssh``` to check out whether I have a existing ssh key. The result is I already have one. Therefore, why do I need to generate a new one, if I already have one? – Jessie Aug 31 '19 at 02:29
  • 9
    Navigate to github profile > Settings > SSH and GPG keys > New SSH key. In the text-field paste the content of `~/.ssh/id_rsa.pub` file. See more at the second link in answer. – Moazzem Hossen Aug 31 '19 at 02:38
  • 5
    I already had a ssh key, and it is encrypted. So I needed to unlock it first by adding it (on a mac) like this: `ssh-add ~/.ssh/id_rsa`, where `id_rsa` is the secret key of your ssh. Then type in my encryption password for it. – Knogobert Dec 29 '20 at 14:23
  • On **Ubuntu** after running `ssh-add id_rsa` I received the error _could not open a connection to your authentication agent_ If you ever run into this . Run 1) `exec ssh-agent bash` 2) `ssh-add ` .) [video](https://www.youtube.com/watch?v=_px5jRgxF3Q) – MattJamison Nov 24 '21 at 19:06
  • For some weird reason, suddenly I can't reach gh with ssh any more, following the step @Knogobert suggested helps me. So weird. Because I remember I did it already, for sure. – Sang Dang May 04 '22 at 07:39
42

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.

stichResist
  • 535
  • 4
  • 2
  • 1
    after failing to push i tried many solutions but this worked for me also after transforming my key to new open ssh format with putty gen – klys Dec 02 '21 at 07:50
  • 1
    This. And for a new key make sure to add it on the server: `$ eval "$(ssh-agent -s)"` and `$ ssh-add path/to/your/key`. – kontur Jul 05 '22 at 13:29
  • 2
    This was all I needed to do, as I had a non-standard name – tekiegirl Aug 17 '22 at 09:30
23

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

Fixed it by running this command

ssh -vT git@github.com

Eric
  • 940
  • 12
  • 27
  • 5
    Thank you very much! It showed the error logs and found out that git uses specific path (~/.ssh) and filename (id_) when trying to authenticate. I shouldn't have renamed that. – Mon Apr 05 '21 at 05:51
  • 1
    Thanks, this solved my problem with the Github today. – Arefe Aug 19 '21 at 08:12
  • Great stuff! Thanks. I worked down through all of the above and finally this got me going... – DrJMcAuliffe Feb 24 '23 at 10:49
2

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/
buddemat
  • 4,552
  • 14
  • 29
  • 49
Digi Jeff
  • 169
  • 1
  • 4
  • 15
1

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?

shrfu31
  • 11
  • 2
1

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

vSzemkel
  • 624
  • 5
  • 11
1

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

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.

enter image description here

nPcomp
  • 8,637
  • 2
  • 54
  • 49
1

I had this problem because i have a ssh key to bitbucket.org and i am trying to add a new one for github.com.

So the solution was:

  • Configure a new ssh-key
  • To delegate which key you must use to conect with github.com, update or create your SSH configuration file (~/.ssh/config) with the following setting:
## My current config
 Host bitbucket.org
 AddKeysToAgent yes
 IdentityFile ~/.ssh/keyOfBitbucket
## End

 Host github.com
 AddKeysToAgent yes   
 IdentityFile ~/.ssh/keyOfGithub
0

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.

GeorgiG
  • 1,018
  • 1
  • 13
  • 29
0

I also faced the same problem while trying to push my files in Github from remote repository. This following steps helped to solve the problem:

  1. ssh-keygen -t rsa -b 4096 -C "your_email@example.com" Use this command in Git bash to create a new ssh key. Enter your email which you have used in your Github account.
  2. A new ssh key will be created with file names id_rsa. Press enter
  3. Again it will ask you to create a passphrase, just press enter twice
  4. clip < ~/.ssh/id_rsa.pub enter this command to copy the ssh key
  5. Now goto github -> settings -> SSH and GPG keys -> new ssh give a title and paste the copied key
Max Cascone
  • 648
  • 9
  • 25