Pushing Code to Github Using Key Pairs

Pushing Code to Github Using Key Pairs

Last updated:

After creating a public repository on github, you need to go to the directory you want to push and do(replace and with your github username and repository name, respectively.):

$ git remote add github git@github.com:<username>/<repo-name>.git

then do a pull to synch stuff:

$ git pull github master

then push:

$ git push github master

P.S.: I'm assuming you have already created a public/private key pair on github and pasted the private key on your OS. You shouldn't be prompted for username and password.

If you don't have a pair of keys yet, use a method like ssh-keygen (if you're on Ubuntu, for instance), which will make you a private key and a public key. Then go to Account-Settings > SSH Keys and paste your public key (the file ending in .pub).

See also this stackoverflow answer explaining how to switch from a https-based url to a ssh-based one when connecting to your github repo.

Troubleshooting: Permission denied (publickey)

If you get an error message such as the following:

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

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

when trying to push to github, it may be because your system hasn't picked up your key from the keychain. To find that, you need to do the following:

  • Make sure the ssh-agent is up and running:

    $ eval "$(ssh-agent -s)"

    Private keys are not the files that end in .pub. These are public key files

  • Manually add your private key file (change my_private_key_name to match your own file name)

    $ ssh-add ~/.ssh/my_private_key_name
  • After doing these steps, try running git pull/push again.

See also

Dialogue & Discussion