方法1:启用并启动SSH-Agent服务

以管理员身份运行PowerShell

  1. 按 Win + X,选择 Windows PowerShell (管理员)

  2. 执行以下命令:

powershell

# 1. 设置SSH-Agent服务为自动启动
Set-Service ssh-agent -StartupType Automatic

# 2. 启动服务
Start-Service ssh-agent

# 3. 检查服务状态
Get-Service ssh-agent

# 4. 添加SSH密钥
ssh-add ~/.ssh/id_ed25519

# 5. 显示公钥
Get-Content ~/.ssh/id_ed25519.pub

这个是方法一正确运行的样子

方法2:如果方法1失败,使用替代方案

使用Git自带的ssh-agent

在Git Bash中操作(不需要Windows服务):

  1. 打开 Git Bash(不是CMD或PowerShell)

  2. 执行:

bash

# 启动ssh-agent
eval "$(ssh-agent -s)"

# 添加密钥
ssh-add ~/.ssh/id_ed25519

# 显示公钥
cat ~/.ssh/id_ed25519.pub

# 测试连接
ssh -T git@github.com

手动启动ssh-agent(不需要服务)

cmd

# 在CMD中
set SSH_AUTH_SOCK=%TEMP%\ssh-agent.sock
ssh-agent.exe -a %SSH_AUTH_SOCK%

# 然后在新的CMD窗口设置相同的环境变量
set SSH_AUTH_SOCK=%TEMP%\ssh-agent.sock
ssh-add %USERPROFILE%\.ssh\id_ed25519

方法3:最简单的方法 - 直接使用密钥文件

如果ssh-agent无法工作,可以直接在克隆时指定密钥:

cmd

# 方法A:设置环境变量
set GIT_SSH_COMMAND=ssh -i %USERPROFILE%\.ssh\id_ed25519

# 然后克隆(使用SSH URL)
git clone git@github.com:ArduPilot/MissionPlanner.git MissionPlanner-master

或者:

cmd

# 方法B:使用Git配置指定密钥
git config --global core.sshCommand "ssh -i %USERPROFILE%\.ssh\id_ed25519"

# 然后克隆
git clone git@github.com:ArduPilot/MissionPlanner.git MissionPlanner-master

方法4:一次性解决方案(推荐)

创建一个批处理文件 clone_with_ssh.bat

batch

@echo off
echo 使用SSH密钥克隆MissionPlanner仓库...
echo.

:: 设置SSH命令使用指定的密钥
set GIT_SSH_COMMAND=ssh -i "%USERPROFILE%\.ssh\id_ed25519"

:: 设置Git使用SSH
git config --global url."git@github.com:".insteadOf "https://github.com/"

:: 克隆仓库(使用SSH地址)
git clone git@github.com:ArduPilot/MissionPlanner.git MissionPlanner-master

if errorlevel 1 (
    echo 克隆失败,尝试使用HTTPS...
    git clone https://github.com/ArduPilot/MissionPlanner.git MissionPlanner-master
) else (
    echo 克隆成功!
)

pause

方法5:验证和测试SSH连接

在PowerShell中测试(不需要启动服务):

powershell

# 测试SSH连接(使用指定密钥)
ssh -i $HOME\.ssh\id_ed25519 -T git@github.com

# 或者使用更详细的测试
ssh -i $HOME\.ssh\id_ed25519 -vT git@github.com

方法6:Windows注册表启用SSH-Agent服务

如果服务确实被禁用,可以通过注册表启用:

  1. 按 Win + R,输入 regedit

  2. 导航到:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ssh-agent

  3. 找到 Start 项,将其值从 4(禁用)改为 2(自动启动)或 3(手动启动)

  4. 重启计算机或在管理员PowerShell中执行:

powershell

Start-Service ssh-agent

当前最可行的解决方案

既然您已经生成了SSH密钥,最简单的方法是:

步骤1:复制公钥到GitHub

powershell

# 在PowerShell中
cat ~/.ssh/id_ed25519.pub
# 复制输出的内容到剪贴板

步骤2:添加到GitHub

  1. 访问 https://github.com/settings/keys

  2. 点击 "New SSH key"

  3. 粘贴公钥内容

步骤3:使用SSH克隆(无需ssh-agent)

cmd

# 在CMD中直接使用
git clone -c core.sshCommand="ssh -i %USERPROFILE%\.ssh\id_ed25519" git@github.com:ArduPilot/MissionPlanner.git MissionPlanner-master

备用方案:使用HTTPS + 凭据管理器

如果SSH仍然有问题,可以切换回HTTPS并使用Git凭据管理器:

cmd

# 切换回HTTPS
git config --global --unset url."git@github.com:".insteadOf "https://github.com/"

# 启用凭据管理器
git config --global credential.helper manager

# 使用浅克隆
git clone --depth 1 https://github.com/ArduPilot/MissionPlanner.git MissionPlanner-master

推荐的操作顺序:

  1. 首先尝试最简单的方法

    cmd

    git clone -c core.sshCommand="ssh -i %USERPROFILE%\.ssh\id_ed25519" git@github.com:ArduPilot/MissionPlanner.git MissionPlanner-master
  2. 如果失败,使用Git Bash

    • 打开Git Bash

    • 执行:git clone git@github.com:ArduPilot/MissionPlanner.git MissionPlanner-master

  3. 再不行,回退到HTTPS

    cmd

    git config --global http.postBuffer 524288000
    git clone --depth 1 https://github.com/ArduPilot/MissionPlanner.git MissionPlanner-master

还有一篇是教如何在下一次使用ssh下载github上的方法,可以进入我的主页找一下,如果有帮助,希望可以给个小星星

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐