2

I've cloned a private repo, forked it and made som changes into a feature branch. When I try to push these changes into the repo i get the following message:

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

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

I've tried to delete the git credentials from my windows computer controlpanel>credentials but it's still showing me the same error.

Is there any chance that I have to log in and out from my terminal to git? If that would be the case, how do I do that?

I've also tried to push with git push --set-upstream origin but it's just showing me the same error.

Have anyone of you seen this before?

Thanks beforehand, Erik

erikos93
  • 677
  • 2
  • 12
  • 26
  • Check if your public key exists. – LF00 Oct 17 '19 at 09:56
  • How do I do that? Tried to paste " ls -al ~/.ssh " into Git bash and got the following: " drwxr-xr-x 1 40114592 1049089 0 Oct 17 11:03 . drwxr-xr-x 1 40114592 1049089 0 Oct 17 11:56 .. -rw-r--r-- 1 40114592 1049089 799 Oct 17 11:03 known_hosts " – erikos93 Oct 17 '19 at 09:59
  • You should check if your local device has the `id_rsa.pub`, then check if the device host the repo have the publickey content in file known_hosts. – LF00 Oct 17 '19 at 10:02
  • you can always ssh into your gitserver `ssh git@gitserver` and see if your key is associated with any user – yellowsir Mar 05 '22 at 12:53

4 Answers4

8

"Help, I keep getting a 'Permission Denied (publickey)' error when I push!"

This means, on your local machine, you haven't made any SSH keys. Not to worry. Here's how to fix:

  1. Open git bash (Use the Windows search. To find it, type "git bash") or the Mac Terminal. Pro Tip: You can use any *nix based command prompt (but not the default Windows Command Prompt!)
  2. Type cd ~/.ssh. This will take you to the root directory for Git (Likely C:\Users[YOUR-USER-NAME].ssh\ on Windows)
  3. Within the .ssh folder, there should be these two files: id_rsa and id_rsa.pub. These are the files that tell your computer how to communicate with GitHub, BitBucket, or any other Git based service. Type ls to see a directory listing. If those two files don't show up, proceed to the next step. NOTE: Your SSH keys must be named id_rsa and id_rsa.pub in order for Git, GitHub, and BitBucket to recognize them by default.
  4. To create the SSH keys, type ssh-keygen -t rsa -C "your_email@example.com". This will create both id_rsa and id_rsa.pub files.
  5. Now, go and open id_rsa.pub in your favorite text editor (you can do this via Windows Explorer or the OSX Finder if you like, typing open . will open the folder).
  6. Copy the contents--exactly as it appears, with no extra spaces or lines--of id_rsa.pub and paste it into GitHub and/or BitBucket under the Account Settings > SSH Keys. NOTE: I like to give the SSH key a descriptive name, usually with the name of the workstation I'm on along with the date.
  7. Now that you've added your public key to Github and/or BitBucket, try to git push again and see if it works. It should!

More help available from GitHub on creating SSH Keys.

Pramod
  • 666
  • 3
  • 11
4

This can be your ssh key is not authorised - Permission denied (publickey).

Solution 1

Check if GitHub protocols are not from ssh to https

check remote url

git remote -v`

Change remote url

git remote set-url origin https://URL

Solution 2

  • Generate a SSH key on your computer
  • Login to your GitHub account
  • Add the newly generated SSH Key to your account through this link
  • Try again to git clone the project.

if after setting ssh key it still fails you might require system restart

Solution 3

This is not a solution just work around for newbies to clone public repo without caring protocols

Use this example

git clone https://github.com/%REPOSITORYFOLDER%/%REPOSITORYNAME%.git

Instead of

git clone git@github.com:%REPOSITORYFOLDER%/%REPOSITORYNAME%.git

all these solutions are collected from different sources will update it if more solution found which works for such scenarios

Hanzla Habib
  • 3,457
  • 25
  • 25
1

This is usually caused by duplicate ssh key on your machine or its complete absence.

$ cat ~/.ssh/id_rsa.pub This should return an ssh key, if output is empty, use below command to create ssh key(Linux/Mac):

$ ssh-keygen Then fetch the key using this command cat ~/.ssh/id_rsa.pub

This is your SSH key. Copy and add this key to your SSH keys in on git. To navigate:
click on photo in top right of any page -> settings -> SSH and GPG Keys -> add SSH Key and add the key

Davies Odu
  • 3,939
  • 1
  • 9
  • 4
0

looks like the same issue I was facing. so here's the way I resolved the same

Step 1:

create a new ssh key:-

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

Step 2:

copy the ssh key on clipboard :-

pbcopy < ~/.ssh/id_ed25519.pub 

Step 3:

now paste copied ssh key to the corresponding git repository

Step 4: Start the ssh-agent in the background.

$ eval "$(ssh-agent -s)"
> Agent pid 59566

Step 5: now try accesing the repo

git clone git@github.com:username/repo-name.git

Step 6:

if still you see the issue then check the ssh config file

vim ~/.ssh/config

the content should look like

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

Step 7:

Add your SSH private key to the ssh-agent and store your passphrase in the keychain

ssh-add --apple-use-keychain ~/.ssh/id_ed25519

Step 8:

Now try again it should work

git clone git@github.com:username/repo-name.git
officialrahulmandal
  • 2,473
  • 1
  • 23
  • 31