Github really seems to want us to use the HTTPS protocol to use Gists, for example, they only list the HTTPS url on a Gist page - e.g. https://gist.github.com/donatello/5834862
Is it possible to clone a Gist using SSH protocol?
Github really seems to want us to use the HTTPS protocol to use Gists, for example, they only list the HTTPS url on a Gist page - e.g. https://gist.github.com/donatello/5834862
Is it possible to clone a Gist using SSH protocol?
Yes, it is possible:
git clone git@github.com:5834862.git
Just replace with your own Gist ID of course.
The dropdown on gist pages now has Embed/Share/Clone HTTPS/Clone SSH options (but the SSH option is only shown when you're logged in):
which show the non-obvious trick is omitting the user name:
Clone HTTPS:
https://gist.github.com/b6f4a53fac485f75afb9150d03efb2f6.git
Works for me with or without .git
, and with or without the username:
https://gist.github.com/cben/b6f4a53fac485f75afb9150d03efb2f6
(as usual on github, the canonical browsing URL works for git too)
Clone SSH:
git@gist.github.com:b6f4a53fac485f75afb9150d03efb2f6.git
AKA
ssh://git@gist.github.com/b6f4a53fac485f75afb9150d03efb2f6.git
Works for me with or without .git
, but doesn't work with username.
I enabled github 2FA which makes HTTPS painful so I always want SSH;
the following ~/.gitconfig
does the translation for all gists on push:
[url "ssh://git@gist.github.com/"]
# In case I just copy-pasted with username:
# [only works for my (cben) gists, but those are the ones I can push]
pushInsteadOf = https://gist.github.com/cben/
# For gists cloned with official no-username URL:
pushInsteadOf = https://gist.github.com/
And for regular (non-gist) repos:
[url "ssh://git@github.com/"]
pushInsteadOf = https://github.com/
[url "ssh://git@bitbucket.org/"]
pushInsteadOf = https://bitbucket.org/
[url "ssh://git@gitlab.com/"]
pushInsteadOf = https://gitlab.com/
P.S. a handy easy way to debug insteadOf
and pushInsteadOf
configs is run git remote -v
, it shows the effective URLs fetch/push will use.
https://help.github.com/articles/which-remote-url-should-i-use#ssh-readwrite---gitgithubspanspancom
git@..... is the ssh protocol
when you copy the clone url for a gist it shows you the https clone url
https://gist.github.com/5834862.git
change https://
to git@
and /****.git
to :****.git
so in this case
git clone git@gist.github.com:5834862.git
If you want, you could grab this script and put it somewhere in your $PATH
. Once that is done, you can do the following:
gist.github.com
using HTTPS
(or if you have an already cloned gist, just proceed to the next step)git-change-url --to-ssh
Now, provided that your public key is uploaded to your github account (it should be listed here) you should be able to work with the gist via SSH
, without having to enter your github credentials.
Much less error-prone than editing git config files by hand.
Ps: If you find any bugs in the script, or have any additions to make, feel free to fork :D
Change https://
to ssh://git@
should do the trick, that is, change
https://gist.github.com/donatello/5834862
to
ssh://git@gist.github.com/donatello/5834862
so git clone ssh://git@gist.github.com/...
should clone the project (if you have already added SSH key on Github)
In my personal opinion, the official document is unclear about SSH.
In order for it to work, one gotta remove the username from the path, leaving only the hash / numbers alone.
SSH and HTTPS links to clone the gist can be copied directly through GitHub's interface, as shown by Beni Cherniavsky-Paskin's answer
However the gist is going to be put in a git directory with that ugly hash as name.
This can be avoided by specifying a directory name when cloning:
git clone git@gist.github.com:9e7f15ae79acd6b60519ef094f669b26.git speedtest-loop