+1 vote
in General IT Tips by (59.8k points)
edited by

When I run "git push  origin main" command to push my codes to GitHub, I get the following error:

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.

I am using an Ubuntu machine. How to fix this error?

1 Answer

+2 votes
by (353k points)
edited by
Best answer

The error message shows that the SSH public key on your local machine does not match the SSH key on your GitHub account. So, you need to delete the old SSH key from your GitHub account and add the SSH public key from your local machine to the GitHub account.

Here are the steps to fix the error:

  • Change directory (cd) to the ".ssh" folder in your home directory. If you have generated an SSH key pair, the SSH public key (id_rsa.pub) should be in this folder. If you have not generated SSH key pair, read the instructions to generate RSA SSH key pair here.

 cd ~/.ssh/

  •  Run the following command to get the SSH public key. Copy the complete key, including "ssh-rsa".

cat id_rsa.pub

  • Log into your GitHub account. Go to Account Settings > SSH and GPG keys. Under "SSH Keys", click on the "New SSH key" button and paste the SSH public key in the "Key" text area. Also, give some title to the key, e.g., "office computer" or "home computer", etc. Click on the "Add SSH key" button.
  • Close the terminal and reopen it. Now, you should not get the error while running the "push" command.

Related questions