Skip to content

Instantly share code, notes, and snippets.

@developius
Last active February 29, 2024 20:24
Show Gist options
  • Save developius/c81f021eb5c5916013dc to your computer and use it in GitHub Desktop.
Save developius/c81f021eb5c5916013dc to your computer and use it in GitHub Desktop.
Setup SSH keys for use with GitHub/GitLab/BitBucket etc

Create a new repository, or reuse an existing one.

Generate a new SSH key:

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

Copy the contents of the file ~/.ssh/id_rsa.pub to your SSH keys in your GitHub account settings (https://github.com/settings/keys).

Test SSH key:

$ ssh -T git@github.com
Hi developius! You've successfully authenticated, but GitHub does not provide shell access.

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 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.

@OtiZ2
Copy link

OtiZ2 commented Sep 27, 2017

Clone can by direct via ssh
git clone git@github.com:username/your-repository.git

@ragerdl
Copy link

ragerdl commented Oct 19, 2017

Given the recent factorability of rsa-2048, it's probably wise to change the first step to include -b 4096 as a flag,

@AndrewPlewe
Copy link

You can test your RSA keys here: https://keychest.net/roca -- I just generated a 2048 bit key using ssh-keygen and it's not vulnerable.

@AndrewPlewe
Copy link

...Also, from here: https://crocs.fi.muni.cz/public/papers/rsa_ccs17

"Q: Is the RSA algorithm insecure in general after the disclosure of this vulnerability?
A: No. If the RSA primes are generated as a truly random numbers, our method cannot be applied. For example, keys generated by the OpenSSL library are NOT affected."

@MartinX3
Copy link

Thank you for this gist!

It is recommend to use the 4096 bit keys.
You don't know, if there will be bugs found, which will weak your key.
You don't know, if there will be a breakthrough in computer performance.

But later, you should switch to elliptic curve keys.
They are smaller, but stronger compared to RSA.

@jbarros35
Copy link

fatal: No such remote 'origin'

@saikumar22k
Copy link

Can I push my changes using shh, if I clone my project using http. Please help

Copy link

ghost commented Mar 24, 2019

Can I push my changes using shh, if I clone my project using http. Please help

yes, you can.
in this answer, I'll assume that you have already taken the steps above, and set up your ssh keys with github.
First check if there is a remote in your repository and its name using:
git remote
if there is one, let's assume it's origin
Check the url of that remote, with:
git remote get-url origin
if it was an http address , then change it to an ssh url using the following cmd:
git remote set-url origin git@github.com:username/repository-name

@lukependleton
Copy link

@HassanElanabi Thank you so much! This has been causing me problems for a long time!

One other thing is that if
git remote get-url origin
doesn't work, you can use
git remote show origin
to see the current url. At least that's how I had to do it. Thanks again!

@troccoli
Copy link

troccoli commented May 1, 2019

I have created the key, added to my GitHub and in fact ssh -T git@github.com responds with Hi troccoli! You've successfully authenticated, but GitHub does not provide shell access.. However, when I try to push I'm still asked for a username and password. And now I can't use that method either because I have enable 2FA (or at least I think that's why).

BTW, I have tried to use the token rather than my password but it didn't work.

Update May 2nd, 2019
I have switched my remotes to ssh instead of https and since I had already installed my public keys on GitHub I had no problem pushing.

@jaeaess
Copy link

jaeaess commented Jun 29, 2019

Hi GitHub community! I am very beginner, so please answer with very detailed help. I will try to be as precise as possible.

Context: Mac OS X El Capitan Version 10.11.6 (15G22010)
GitHub Page with 2 Google Domains: jaeaess.com and jessdejesus.com

I am having 2 different GitHub accounts:
jaeaess -> jaeaess@gmail.com
jessdejesus -> i.am.jess.dejesus@gmail.com

At the beginning, I connected my account "jaeaess" with HTTPS. Adding, pushing committing were working fine. Then, I connected my account "jessdejesus" with HTTPS. But then I ran into errors when trying to push my code...

In order to be able to add, commit, and push from both of my accounts, I decided to follow https://medium.com/@therajanmaurya/git-push-pull-with-two-different-account-and-two-different-user-on-same-machine-a85f9ee7ec61
For both of my accounts.

Now, I am having the following:

#jessdejesus account
Host github-jessdejesus
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github_jessdejesus"

  • Another config file, located in /Users/jess/.ssh/config, containing:
    "#Default GitHub
    Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_github_jaeaess

#Second GitHub
Host github.com-jessdejesus
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github_jessdejesus"

However, now that I did this, pushing my code doesn't work anymore for both of my accounts...
I get the following errors:
"For jaeaess:
Jessicas-MacBook-Pro:websites jess$ cd jaeaess/
Jessicas-MacBook-Pro:jaeaess jess$ git add .
Jessicas-MacBook-Pro:jaeaess jess$ git commit -m "test jaeaess"
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
Jessicas-MacBook-Pro:jaeaess jess$ git push -u origin master
To https://github.com/jaeaess/jaeaess.github.io.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/jaeaess/jaeaess.github.io.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details."

For jessdejesus:
Jessicas-MacBook-Pro:jessdejesus jess$ git add .
Jessicas-MacBook-Pro:jessdejesus jess$ git commit -am "second commit"
On branch master
nothing to commit, working tree clean
Jessicas-MacBook-Pro:jessdejesus jess$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream origin master

Jessicas-MacBook-Pro:jessdejesus jess$"

Could you please help me?
Many thanks in advance...

@lydiaralph
Copy link

lydiaralph commented Aug 9, 2019

@jaeaess the user in those SSH config files should be your GitHub username, jaeaess

Host github.com HostName github.com User jaeaess IdentityFile ~/.ssh/id_rsa_github_jaeaess

Also make sure you have done git init and set the remote URL to your repository (Step 4)

@AquilineAdaeze
Copy link

I have successfully created my SSH keys and when I tested them I got this message "Hi Aquiline Adaeze! You've successfully authenticated, but GitHub does not provide shell access". I have tried to push my files and I am still asked for my username and password. When I provide the details I get an error message that says remote : Invalid username or password and fatal : Authentication failed for 'https://github.com/AquilineAdaeze/gitformyMac/

Please help me!!! I am completely new to this and I am at my wits end with this challenge and I do not know what to do again. Thank you very much in advance.

@developius
Copy link
Author

developius commented Aug 18, 2019

@AquilineAdaeze you need to change the origin to use SSH rather than HTTPS.

Easiest way is to use the command line:

$ git remote remove origin
$ git remote add origin git@github.com:AquilineAdaeze/gitformyMac.git

@tiagosimoesdev
Copy link

thanks it worked !

@arif98741
Copy link

really awesome tricks

@curbengh
Copy link

curbengh commented Oct 4, 2019

If anyone using this in CI or script with set -e enabled, do note ssh -T git@github.com exits with non-zero, which prevent subsequent commands (like git push) from running. You can force exit code zero by using,

$ ssh -T git@github.com || :

Or in YML,

  - ssh -T git@github.com || ":"

@wasit-shafi
Copy link

tnks..

@ajsaraujo
Copy link

This could have a link to a tutorial showing how to setup the SSH keys on your account, had to search this elsewhere. Anyways, thanks for the gist!

@janek
Copy link

janek commented Dec 10, 2020

Very useful! Maybe consider including the link to https://github.com/settings/keys? That would make the instructions even quicker to use, and the url will probably remain the same for a while

@developius
Copy link
Author

@ajsaraujo @janek done 😄

@punam-shinde
Copy link

git remote set-url origin git@github.com:User name /repo.git
this command really works me. thank you.

@hspeight
Copy link

THX

@chandrumani
Copy link

Easiest way is to use the command line:

Thanks, it worked.

@argiepiano
Copy link

Thanks for the directions. I'm trying this in a shared hosting account. When I try testing the key with ssh -T git@github.com I get:
/bin/sh: /usr/bin/sss_ssh_knownhostsproxy: No such file or directory
ssh_exchange_identification: Connection closed by remote host

Can you help? Is it not possible to set up a SSH key in shared hosting shell?

@developius
Copy link
Author

@argiepiano it looks like your server doesn't have the ssh command configured correctly - you may need to get in touch with your hosting provider as sss_ssh_knownhostsproxy looks like something weird on their end.

@ayyzenn
Copy link

ayyzenn commented Sep 9, 2021

Thanks for the guidance but after following the steps it is still asking for the username and password with pushing.

@muX1337
Copy link

muX1337 commented Nov 4, 2021

Thanks for the guidance but after following the steps it is still asking for the username and password with pushing.

had the same issue. It helped when I switched to the ssh and not https, Moreover I've added my public-key not to my account settings instead I've added my key to the repo with special rights, I've just created my repo so with a fix new git clone everything was fine but if you had bigger stuff and some branches etc. maybe script this will help https://gist.github.com/m14t/3056747

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