Skip to content

Instantly share code, notes, and snippets.

@xirixiz
Forked from developius/README.md
Last active March 12, 2024 19:22
Show Gist options
  • Save xirixiz/b6b0c6f4917ce17a90e00f9b60566278 to your computer and use it in GitHub Desktop.
Save xirixiz/b6b0c6f4917ce17a90e00f9b60566278 to your computer and use it in GitHub Desktop.
Set up GitHub push with SSH keys

SSH keypair setup for GitHub (or GitHub/GitLab/BitBucket, etc, etc)

Create a repo.

Make sure there is at least one file in it (even just the README.md)

Generate a SSH key pair (private/public):

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

or even better:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

It also possible to use ed25519. There are pros and cons, but personally I've had some issues and that is the reason I've chosen to stick to 4096 rsa for now.

Copy the contents of the public SSH key

macOS:

pbcopy < ~/.ssh/id_rsa.pub

GNU/Linux (requires the xclip package):

xclip -sel clip < ~/.ssh/id_rsa.pub

Windows Command Line:

type %userprofile%\.ssh\id_rsa.pub | clip

Git Bash on Windows / Windows PowerShell:

cat ~/.ssh/id_rsa.pub | clip

or ofcourse copy it via your favorite editor, cat, or whatever suits your needs :)

Copy the public SSH key to GitHub

Copy the contents of the to your SSH keys to your GitHub account settings (https://github.com/settings/keys).

Test the SSH key:

ssh -T git@github.com

Change directory into the local clone of your repository (if you're not already there) and run:

git remote set-url origin git@github.com:username/your-repository.git

Now try editing a file (try the README) and then do:

git add -A
git commit -am "Update README.md"
git push

Add the key to the ssh-agent

ssh-add ~/.ssh/id_rsa

You should not be asked for a username or password. If it works, your SSH key is correctly configured.

@fidodido48
Copy link

Clear message, very helpful. Thank you.

@njfamirm
Copy link

njfamirm commented Oct 9, 2022

thanks!

@RollsChris
Copy link

RollsChris commented Oct 11, 2022 via email

@jaibrat
Copy link

jaibrat commented Jan 6, 2023

This is missing: ssh-add ~/.ssh/id_rsa
(adding private key to your SSH-agent) As described here: [https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent]
Or else you'll get this error: "Permission denied (publickey)"

@xirixiz
Copy link
Author

xirixiz commented Jan 7, 2023

@jaibrat updated. Thanks!

@ezoer
Copy link

ezoer commented Feb 5, 2023

Thank you. I had almost everything working but had missed this part:

git remote set-url origin git@github.com:username/your-repository.git

@billh93
Copy link

billh93 commented Mar 13, 2023

This is really helpful. Thank you for putting the time in for the tut!

@SavSanta
Copy link

When troubleshooting a really unconventional setup of a local gitlab in our environment and with keys configured I found this command on StackOverflow helped me determine that a nonstandard private key/identity file (ie not named id_rsa, id_ecdsa, etc) in the .ssh folder caused the auth to gitlab to fail. The easy fix in my case was to copy the private key into an id_rsa file. Im sure there is an option to specify a specific identity file but I havent checked the manpages yet.

Anyway the verbosity alias for git I used is git -c core.sshCommand="ssh -v" push -u origin master

@synevir
Copy link

synevir commented Apr 12, 2023

Thanks a lot, xirixiz.
The native github doc with port 443 is sucks.

@ramithuh
Copy link

ramithuh commented May 6, 2023

Thanks, I was missing this as well!

Thank you. I had almost everything working but had missed this part:

git remote set-url origin git@github.com:username/your-repository.git

@softwareshinobi
Copy link

If you keep getting prompted for a username and password after following these instructions...

Clone your project using SSH and not HTTPS. (or i guess change the config file in the .git directory).

Then push.

@fanfansmilkyway
Copy link

Thank you very much.

@ferrum-the-beautiful
Copy link

thank you!

@Fat83dotcom
Copy link

Thank you!!!

@Khant-Nyar
Copy link

.

@JolienRietkerk
Copy link

If you keep getting prompted for a username and password after following these instructions...

Clone your project using SSH and not HTTPS. (or i guess change the config file in the .git directory).

Then push.

this helped, thanks!

@biswatig
Copy link

I was using https repo endpoints.. if your using SSH you need the SSH end points. use them when setting origin

You just SAVED my life ! Thanks a ton !

@phetdam
Copy link

phetdam commented Aug 13, 2023

Saved me--I only needed to run the git remote set-url. Thanks!

@princedikko
Copy link

did not solve my problems, github seems to be changing methods to the extent even full stack developers are complaining, i have done everything correctly but still error

@cmaxreilly
Copy link

You are a wonder, thank you!

@thomas15503
Copy link

I cant get past the test part. I type this in : ssh -T git@github.com

and it said Permission Denied (Public Key)

@phetdam
Copy link

phetdam commented Jan 15, 2024

I cant get past the test part. I type this in : ssh -T git@github.com

and it said Permission Denied (Public Key)

Don't forget to ensure the SSH agent is already running and that your GitHub private SSH key has been added to the agent. There is relevant GitHub documentation that provides more details on how to start the SSH agent in the background and add your private key. You can start the SSH agent in the background using eval "$(ssh-agent -s)" and then add the GitHub private key with

ssh-add /path/to/github/privatekey 

You will be prompted for the private key's password if it has one. After these steps, ssh -T git@github.com should work and give you

Hi <your username>! You've successfully authenticated, but GitHub does not provide shell access.

@thomas15503
Copy link

thomas15503 commented Jan 16, 2024 via email

@thomas15503
Copy link

thomas15503 commented Jan 16, 2024 via email

@phetdam
Copy link

phetdam commented Jan 17, 2024

It’s still not working. Idk what’s going on

What's the error you are getting? Is it still a permission issue with the public key?

Don't forget to also ensure that the SSH public key is already added to your GitHub account.

@thomas15503
Copy link

thomas15503 commented Jan 17, 2024 via email

@phetdam
Copy link

phetdam commented Jan 27, 2024

Sorry for the late response--have been recovering from an illness for nearly 2 weeks.

The public SSH key should be easy to copy via your browser... so far it seems like that's the problem. Unless you have figured it out already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment