5

How can I push my repository to Github using SSH?

Why

I want to host my repository on GitHub during my PHP study.

What I did

I set remote url of kaede0902/php to htdocs/workspace, and tried to pull. I used sudo.

Error Message

kaede@kaede-ThinkPad-T450:/opt/lampp/htdocs/workspace$ sudo git pull origin master
Warning: Permanently added the RSA host key for IP address '52.69.186.44' to the list of known hosts.
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.

My ssh url is git@github.com:kaede0902/php.git.

I am using ssh connection for other repos.

What is the problem?

M. Twarog
  • 2,418
  • 3
  • 21
  • 39
KAEDE
  • 340
  • 3
  • 10

1 Answers1

11

You have to add your ssh key to both machines.

The error means that you don't have the ssh on your machine.
You can copy the existing one form any of your machines or create a new one.


Creating and adding ssh key

Source: https://help.github.com/en/enterprise/2.15/user/articles/adding-a-new-ssh-key-to-your-github-account

Generating ssh key

  • Open Git Bash.

  • Run ssh-keygen and follow the on-screen messages (or simply click Enter till it over)

  • Copy the key file (the content of the public key) located in:

       ~/.ssh/id_rsa.pub
    

Add key to Github

  • In the upper-right corner of any page, click your profile photo, then click Settings. enter image description here

  • In the user settings sidebar, click SSH and GPG keys.
    enter image description here

  • Click New SSH key or Add SSH key. enter image description here

  • Paste your key into the "Key" field. enter image description here

  • Click Add SSH key.
    enter image description here

  • change the repository url to ssh

      git remote set-url origin <ssh url>
    
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 3
    to change the repository url to ssh has a bad syntax, the correct one should be git remote set-url origin git@github.com:myGitAccount/myRepo.git – Pepe Alvarez Dec 27 '21 at 19:26