How to clone a Github Gist via SSH protocol?

Yes, it is possible:

git clone [email protected]: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): gist dropdown
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:
    [email protected]:b6f4a53fac485f75afb9150d03efb2f6.git
    AKA
    ssh://[email protected]/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://[email protected]/"]
    # 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://[email protected]/"]
    pushInsteadOf = https://github.com/
[url "ssh://[email protected]/"]
    pushInsteadOf = https://bitbucket.org/
[url "ssh://[email protected]/"]
    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 [email protected]: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:

  1. Clone any gist from gist.github.com using HTTPS (or if you have an already cloned gist, just proceed to the next step)
  2. Anywhere in the gist's git directory tree, run the command
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://[email protected]/donatello/5834862

so git clone ssh://[email protected]/... should clone the project (if you have already added SSH key on Github)

In my personal opinion, the official document is unclear about SSH.