85

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?

donatello
  • 5,727
  • 6
  • 32
  • 56

8 Answers8

128

Yes, it is possible:

git clone git@github.com:5834862.git

Just replace with your own Gist ID of course.

donatello
  • 5,727
  • 6
  • 32
  • 56
28

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

Beni Cherniavsky-Paskin
  • 9,483
  • 2
  • 50
  • 58
17

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

xero
  • 4,077
  • 22
  • 39
2

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

Sankalp
  • 499
  • 7
  • 4
  • Not exactly what the op asked for, but still useful! I didn't want to clone my existing repository so this solution worked for me. I would add `nano $(git rev-parse --git-dir)/config` as an alternative to directly modify the entries manually. So the answer will be more self contained ;). Otherwise you first have to download a script and check if it might do harm to your system and s.o. – isaias-b Apr 22 '16 at 07:12
2

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.

shintaroid
  • 1,556
  • 3
  • 20
  • 34
1

In order for it to work, one gotta remove the username from the path, leaving only the hash / numbers alone.

cnst
  • 25,870
  • 6
  • 90
  • 122
0

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
manero
  • 1
-1

It is possible, Try this:

git clone git@YOURSSHHOST:YOURGISTIDHERE.git
One Mad Geek
  • 469
  • 4
  • 9