How to clone a Github Gist via SSH protocol?

人盡茶涼 提交于 2019-12-02 15:22:31

Yes, it is possible:

git clone git@github.com:5834862.git

Just replace with your own Gist ID of course.

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

The dropdown on gist pages now has Embed/Share/Clone HTTPS/Clone SSH options:


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.

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

It is possible, Try this:

git clone git@YOURSSHHOST:YOURGISTIDHERE.git
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!