【问题标题】:How do I clone a private git repo with ssh in vscode?如何在 vscode 中使用 ssh 克隆私有 git 存储库?
【发布时间】:2020-09-20 23:17:06
【问题描述】:

我正在使用 mac,我想知道如何使用 vscode 克隆带有 ssh 的私有 github 存储库?我查看了 code.visualstudio.com/docs/editor/versioncontrol,但找不到任何关于使用 ssh 进行克隆的信息。

在 vscode 中,我转到左侧的源代码管理图标,然后单击克隆存储库。我输入git@github.com:username/project,它说权限被拒绝。我确实生成了我的公钥和私钥,但我在哪里输入它们?

【问题讨论】:

标签: git github ssh visual-studio-code


【解决方案1】:

Ssh 是您在 ssh-agent 中设置的东西,它适用于 bash。我使用 Vscode 已经有一段时间了,我总是使用 git bash 设置 ssh,这更容易。我使用 git bash 将一个项目克隆到我的工作区,然后使用 vscode 打开它。

据我所知,您无法从 vscode 设置 ssh。

如果您希望从终端设置 ssh,文档为 here

【讨论】:

    【解决方案2】:

    我今天刚刚经历了这个。我假设您已经安装了适用于 Windows 的 Git,并生成了位于用户“.ssh”文件夹中的 ssh 密钥。 之后,您必须在线进入您的 GitHub 帐户中的设置,并在左侧菜单中查找 “SSH 和 GPG 密钥” 选项:

    您必须在此处输入您的公钥,方法是打开 "id_rsa.pub" 文件,然后通过单击“新建 SSH 密钥”将其内容复制到您的 GitHub 帐户:

    然后,您可以像使用常规 HTTP 连接一样使用 vscode。直接Crtl+Shift+P打开vscode的Command Pallet,选择git:clone,粘贴你想要的GitHub repo的SSH地址。为了在您单击 GitHub 存储库上的绿色“克隆或下载”按钮时获得 SSH 地址,您还必须在弹出的框中单击 “使用 SSH 克隆”,以便复制 SSH 地址而不是使用 HTTPS 链接。

    您可能遇到的唯一问题是,第一次在 vscode 中尝试 ssh 时,它会说“无法识别主机”解决此问题的方法是简单地从一个 git clone vscode 之外的 git bash 命令行,因此当它询问您是否可以添加 GitHub 的 SSH 密钥指纹时,您可以键入“yes”。

    ROB@WINDOW10 MINGW64 ~/Documents
    $ git clone git@github.com:Rob4226/example.git
    Cloning into 'Example Repo'...
    The authenticity of host 'github.com (140.82.114.3)' can't be established.
    RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
    Are you sure you want to continue connecting (yes/no/[fingerprint])? **yes**
    Warning: Permanently added 'github.com,140.82.114.3' (RSA) to the list of known hosts.
    

    无论出于何种原因,vscode git 源代码控制 UI 都不会显示此消息以供您响应,因此它只会引发错误。但是在你第一次在 git bash 命令行中说“是”之后,ssh 将从现在开始在 vscode 中正常工作。它工作得很好,而且速度更快!我不再使用 vscode 的 HTTPS。

    或者,您可以手动将 GitHub 的 SSH 密钥指纹添加到 .ssh 文件夹中的 "known_hosts" 文件中。他们可以在这里找到:https://help.github.com/en/github/authenticating-to-github/githubs-ssh-key-fingerprints

    GitHub's SSH key fingerprints
    Public key fingerprints can be used to validate a connection to a remote server.
    
    These are GitHub's public key fingerprints (in hexadecimal format):
    
    16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48 (RSA)
    ad:1c:08:a4:40:e3:6f:9c:f5:66:26:5d:4b:33:5d:8c (DSA)
    These are the SHA256 hashes shown in OpenSSH 6.8 and newer (in base64 format):
    
    SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8 (RSA)
    SHA256:br9IjFspm1vxR3iA35FWE+4VTyz1hYVLIE2t1/CeyWQ (DSA)
    

    【讨论】:

    • 问题是针对 Mac,而不是针对 Windows。我在我的 Mac 上遇到了同样的问题。即使您使用密钥很好地设置了 git,以便您可以克隆到 Mac 上的任何位置,使用全局 ssh 密钥设置,VSCode 也会抱怨“Git: ssh_askpass: exec(/usr/X11R6/bin/ssh-askpass) :没有这样的文件或目录”,放弃。所以它似乎根本不使用全局 Mac ssh 设置
    • 我也有一台 Mac,它运行良好。可能是因为您更新了 Mac OS,这有时会使 git 丢失您的 ssh 密钥的密码。试试ssh-add ~/.ssh/id_rsa,然后重启vscode。
    猜你喜欢
    • 2017-08-07
    • 1970-01-01
    • 2019-01-26
    • 2011-05-18
    • 2022-01-23
    • 2021-02-04
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    相关资源
    最近更新 更多