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

问题描述 投票:56回答:7

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

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

git github gist
7个回答
87
投票

对的,这是可能的:

git clone [email protected]:5834862.git

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


15
投票

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

git @ .....是ssh协议

当您复制gist的克隆URL时,它会显示https克隆URL

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

https://改为git@/****.git改为:****.git

所以在这种情况下

git clone [email protected]:5834862.git


13
投票

gist页面的下拉列表现在具有Embed / Share / Clone HTTPS / Clone SSH选项: 显示非显而易见的技巧是省略用户名:

  • 克隆HTTPS: https://gist.github.com/b6f4a53fac485f75afb9150d03efb2f6.git 无论有没有.git,有或没有用户名:https://gist.github.com/cben/b6f4a53fac485f75afb9150d03efb2f6(通常在github上,规范浏览网址也适用于git)
  • 克隆SSH: [email protected]:b6f4a53fac485f75afb9150d03efb2f6.git AKA ssh://[email protected]/b6f4a53fac485f75afb9150d03efb2f6.git 无论有没有.git,我都可以使用,但不能使用用户名。

我启用了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/

对于常规(非要点)回购:

[url "ssh://[email protected]/"]
    pushInsteadOf = https://github.com/
[url "ssh://[email protected]/"]
    pushInsteadOf = https://bitbucket.org/
[url "ssh://[email protected]/"]
    pushInsteadOf = https://gitlab.com/

附:调试insteadOfpushInsteadOf配置的简便方法是运行git remote -v,它显示了fetch / push将使用的有效URL。


2
投票

如果你愿意,你可以抓住this脚本并把它放在你的$PATH的某个地方。完成后,您可以执行以下操作:

  1. 使用gist.github.com克隆来自HTTPS的任何要点(或者如果你已经克隆了要点,请继续下一步)
  2. 在gist的git目录树中的任何位置运行命令
git-change-url --to-ssh

现在,只要您的公钥上传到您的github帐户(它应该列在here),您应该能够通过SSH使用gist,而无需输入您的github凭据。

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

Ps:如果您在脚本中发现任何错误,或者有任何新增内容,请随意分叉:D


1
投票

https://改为ssh://git@应该做的伎俩,即改变

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

ssh://[email protected]/donatello/5834862

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

在我个人看来,official document不清楚SSH。


1
投票

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


0
投票

有可能,试试这个:

git clone git@YOURSSHHOST:YOURGISTIDHERE.git
© www.soinside.com 2019 - 2024. All rights reserved.