Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"

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

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

@almiavicas
Copy link

almiavicas commented Oct 30, 2021

Thanks! It realy helps the git remote set-url line

@satylogin
Copy link

satylogin commented Nov 13, 2021

Thanks for the writeup :) They should really add the git remote set-url line in official setup.

@tik9
Copy link

tik9 commented Dec 24, 2021

@satylogin git remote set-url origin is clearly and a must be in the doks, look no further

@tik9
Copy link

tik9 commented Jan 25, 2022

What does git remote -v output?

@tusharGitHub13
Copy link

tusharGitHub13 commented Feb 4, 2022

Thanks a lot really helpful to start journey on Github

@rakzhodekams
Copy link

rakzhodekams commented Feb 5, 2022

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

ShukuraliProgrammer commented Jun 2, 2022

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

RoboWanabe commented Jun 6, 2022

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

RoboWanabe commented Jun 6, 2022

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

thangnv-works commented Jun 29, 2022

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

@Endoego
Copy link

Endoego commented Jul 28, 2022

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

Moawyah-AbdulRahman commented Aug 9, 2022

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

vrquaeler commented Sep 16, 2022

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

fidodido48 commented Sep 18, 2022

Clear message, very helpful. Thank you.

@njfamirm
Copy link

njfamirm commented Oct 9, 2022

thanks!

@Endoego
Copy link

Endoego commented Oct 11, 2022

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