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.

@tik9
Copy link

tik9 commented Jan 25, 2022

What does git remote -v output?

@tusharGitHub13
Copy link

Thanks a lot really helpful to start journey on Github

@DaoExpression
Copy link

ssh from github say "hi" .. then I try to push and ask user and pass.. still need to use tokens?

@tik9
Copy link

tik9 commented Feb 5, 2022

@rakzhodekams What code did you use? Usually you need a token, but let me see your code.

@psychoslave
Copy link

psychoslave commented Feb 8, 2022

I had an SSH connection issue, solved thanks to git - ssh: connect to host github.com port 22: Connection timed out - Stack Overflow:

cat >>~/.ssh/config <<CONFIG
Host github.com
 Hostname ssh.github.com
 Port 443
CONFIG

@Viha123
Copy link

Viha123 commented Feb 17, 2022

thank you very much, took longer than it should have but I got there.

@trobutlef
Copy link

trobutlef commented Apr 5, 2022

thank you! I've been struggling with this for hours.

@cnske
Copy link

cnske commented Apr 8, 2022

thank you! How to use this approach with several repos?

Copy link

ghost commented Apr 30, 2022

Thank you. This has improved my workflow

@ShukuraliProgrammer
Copy link

I couldn't solve with this solution

@xirixiz
Copy link
Author

xirixiz commented Jun 3, 2022

Good description @ShukuraliProgrammer. Please be more clear what it is that's not working out for you. Only then the community is able to help out.

@RoboWanabe
Copy link

I'm currently having an issue, followed advice.. created keys, assigned to GitHub, tested connection with ssh. but still when i try to push i'm getting fatal: repository 'https://github.com/user/repo.git/' not found

:O

Any ideas how to debug?

@RoboWanabe
Copy link

just solved my own problem....

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

Allways expanding my memory slots ;)

@thangnv-works
Copy link

Thank you

@ctimmer
Copy link

ctimmer commented Jul 28, 2022

I ran the ssh test and it ran as expected but when I run:
git push
I get the following response:
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/ctimmer/poll-looper/'

Why is the new authentication (ssh) being recognized? What am I missing?

Thanks,
Curt

@RollsChris
Copy link

Do a git remote and make sure is includes SSH URLS.

There can be different endpoints for your repo, one is for the old password now deprecated way (https), the other is for SSH.. you can see the URL as different for both.

Go to your repo and click clone, select SSH, copy these URLS and change your remote on your repo.
Hope this makes sense.

@ctimmer
Copy link

ctimmer commented Jul 29, 2022

Is this what you mean "git remote"? This is all that was displayed:
git remote get-url --all origin
https://github.com/ctimmer/poll-looper

Doesn't appear to be an SSH url. Where do I click "clone"?

Thanks
Curt

@ctimmer
Copy link

ctimmer commented Jul 29, 2022

Got it wasn't using the SSH url. Thanks for the help

Curt

@gstano
Copy link

gstano commented Aug 1, 2022

Hello Everyone,

Thank you very much for this thread. I am a beginner using github. I have successfully used in the past following instructions (and even set up the key in the past). However, I had to re-install everything from scratch recently. I have the keys established, but for some reason, I cannot link them up with my local instance of git and the online instance.

I am following all of the instructions here, but I keep getting asked for a user name and password.

When I do ssh -T git@github.com, I get:
Offending key for IP in ... (gives directory)
Matching host key in ... (gives directory)
Are you sure you want to continue connecting (yes/no)? yes

When I select yes, it does recognize me. As far as I can tell, it appears my old key is trying to be used versus the new key.

It is mildly annoying as I managed this once before, but cannot accomplish the task again, even with the various support threads.

Thank you for your time and help!
Geoffrey

@gstano
Copy link

gstano commented Aug 1, 2022

As a quick follow-up, it is clear that something is not being connected as when I check my SSH keys on github, neither have been used today. So, my problem may be further back than I first thought.

@Moawyah-AbdulRahman
Copy link

I got "Hi Moawyah-AbdulRahman! You've successfully authenticated, but GitHub does not provide shell access.", then tried pushing, but got this massage "remote: Support for password authentication was removed on August 13, 2021.".

@Sy3Omda
Copy link

Sy3Omda commented Aug 29, 2022

I had an SSH connection issue, solved thanks to git - ssh: connect to host github.com port 22: Connection timed out - Stack Overflow:

cat >>~/.ssh/config <<CONFIG
Host github.com
 Hostname ssh.github.com
 Port 443
CONFIG

REALLY thanks, i was struggling to commit using ssh

@vrquaeler
Copy link

REALLY Thank's a lot. I was struggling with ssh for month...
You made my day!!!

@xirixiz
Copy link
Author

xirixiz commented Sep 16, 2022

REALLY Thank's a lot. I was struggling with ssh for month... You made my day!!!

Great!

@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