Skip to content

Instantly share code, notes, and snippets.

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

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

This comment has been minimized.

Copy link

OtiZ2 commented Sep 27, 2017

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

@ragerdl

This comment has been minimized.

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

This comment has been minimized.

Copy link

AndrewPlewe commented Jan 24, 2018

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

This comment has been minimized.

Copy link

AndrewPlewe commented Jan 24, 2018

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

This comment has been minimized.

Copy link

MartinX3 commented Feb 16, 2018

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

This comment has been minimized.

Copy link

jbarros35 commented Jan 13, 2019

fatal: No such remote 'origin'

@saikumar22k

This comment has been minimized.

Copy link

saikumar22k commented Mar 6, 2019

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

@HassanElanabi

This comment has been minimized.

Copy link

HassanElanabi 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

This comment has been minimized.

Copy link

lukependleton commented Mar 24, 2019

@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

This comment has been minimized.

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

This comment has been minimized.

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:

  • 2 ssh keys: id_rsa_github_jaeaess and id_rsa_github_jessdejesus (created with the command ssh-keygen -t rsa -C "jaeaess@gmail.com/i.am.jess.dejesus@gmail.com"), both located in /Users/jess/.ssh/
  • Both keys have been added to their respective GitHub account following https://help.github.com/en/articles/adding-a-new-ssh-key-to-your-github-account
  • A config file located in /Users/jess/Documents/websites/config, containing:
    "#jaeaess account
    Host github.com-jaeaess
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_github_jaeaess

#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

This comment has been minimized.

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

This comment has been minimized.

Copy link

AquilineAdaeze commented Aug 16, 2019

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

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

Copy link

tiagosimoesdev commented Aug 30, 2019

thanks it worked !

@arif98741

This comment has been minimized.

Copy link

arif98741 commented Sep 25, 2019

really awesome tricks

@curbengh

This comment has been minimized.

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 || ":"
@wasitshafi

This comment has been minimized.

Copy link

wasitshafi commented Nov 12, 2019

tnks..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.