git 如何通过 SSH 协议克隆 Github Gist?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18019142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:42:00  来源:igfitidea点击:

How to clone a Github Gist via SSH protocol?

gitgithubgist

提问by donatello

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

Github 似乎真的希望我们使用 HTTPS 协议来使用 Gist,例如,他们只在 Gist 页面上列出 HTTPS url——例如https://gist.github.com/donatello/5834862

Is it possible to clone a Gist using SSH protocol?

是否可以使用 SSH 协议克隆 Gist?

回答by donatello

Yes, it is possible:

对的,这是可能的:

git clone [email protected]:5834862.git

Just replace with your own Gist ID of course.

当然,只需用您自己的 Gist ID 替换即可。

回答by Beni Cherniavsky-Paskin

The dropdown on gist pages now has Embed/Share/Clone HTTPS/Clone SSH options:
gist dropdown
which show the non-obvious trick is omitting the user name:

要点页面上的下拉菜单现在有 Embed/Share/Clone HTTPS/Clone SSH 选项:
要点下拉菜单
这表明不明显的技巧是省略用户名

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

  • 克隆 HTTPS:
    https://gist.github.com/b6f4a53fac485f75afb9150d03efb2f6.git
    无论是否使用.git,无论是否使用用户名都适用于我:( https://gist.github.com/cben/b6f4a53fac485f75afb9150d03efb2f6像往常一样在 github 上,规范浏览 URL 也适用于 git)

  • 克隆 SSH:
    [email protected]:b6f4a53fac485f75afb9150d03efb2f6.git
    AKA 对我有用或不用,但不适用于用户名。
    ssh://[email protected]/b6f4a53fac485f75afb9150d03efb2f6.git
    .git



I enabled github 2FA which makes HTTPS painful so I always want SSH; the following ~/.gitconfigdoes the translation for all gists on push:

我启用了 github 2FA,这让 HTTPS 变得痛苦,所以我总是想要 SSH;以下~/.gitconfig对推送的所有要点进行翻译:

[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 insteadOfand pushInsteadOfconfigs is run git remote -v, it shows the effective URLs fetch/push will use.

PS 一个方便的调试insteadOfpushInsteadOf配置运行方法git remote -v,它显示了获取/推送将使用的有效 URL。

回答by xero

https://help.github.com/articles/which-remote-url-should-i-use#ssh-readwrite---gitgithubspanspancom

https://help.github.com/articles/which-remote-url-should-i-use#ssh-readwrite---gitgithubspanspancom

git@..... is the ssh protocol

git@..... 是 ssh 协议

when you copy the clone url for a gist it shows you the https clone url

当您复制克隆 url 以获取要点时,它会向您显示 https 克隆 url

https://gist.github.com/5834862.git

https://gist.github.com/5834862.git

change https://to git@and /****.gitto :****.git

https://git@/****.git:****.git

so in this case

所以在这种情况下

git clone [email protected]:5834862.git

git clone [email protected]:5834862.git

回答by Sankalp

If you want, you could grab thisscript and put it somewhere in your $PATH. Once that is done, you can do the following:

如果你愿意,你可以抓住这个脚本并将它放在你的$PATH. 完成后,您可以执行以下操作:

  1. Clone any gist from gist.github.comusing 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
  1. gist.github.comusing克隆任何要点HTTPS(或者如果您已经克隆了一个要点,请继续下一步)
  2. 在 gist 的 git 目录树中的任何位置,运行命令
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.

现在,假设您的公钥已上传到您的 github 帐户(应在此处列出),您应该能够通过 使用 gist SSH,而无需输入您的 github 凭据。

Much less error-prone than editing git config files by hand.

比手动编辑 git 配置文件更不容易出错。

Ps: If you find any bugs in the script, or have any additions to make, feel free to fork :D

Ps:如果你发现脚本中的任何错误,或者有任何补充,请随时 fork :D

回答by shintaroid

Change https://to ssh://git@should do the trick, that is, change

https://ssh://git@应该就行了,就是改

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)

所以git clone ssh://[email protected]/...应该克隆项目(如果你已经在 Github 上添加了 SSH 密钥)

In my personal opinion, the official documentis unclear about SSH.

在我个人看来,官方文档对 SSH 的描述不清楚。

回答by cnst

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

为了让它工作,必须从路径中删除用户名,只留下哈希/数字。

回答by One Mad Geek

It is possible, Try this:

有可能,试试这个:

git clone git@YOURSSHHOST:YOURGISTIDHERE.git