在Windows上将git与ssh-agent搭配使用,再也不用输入git密码了

前言

之前的文章中已经解决了 Windows 上的 SSH 登录的问题,成功把 Windows 上的 ssh-agent 用起来了,详情可以看这篇文章: 在 Windows 10 上实现免密码 SSH 登录

不过使用 git 还是会反复提示输入私钥密码,烦得很

原因也很简单,打开 git bash 运行一下命令测试就知道了

bash 复制代码
$ ssh-add -l
Could not open a connection to your authentication agent.

就是 git 里用的 ssh 是它自带的,不是 Windows 系统的那一个,应该是为了兼容性考虑吧,毕竟之前 Windows 是没有 ssh 的

那么解决思路也很简单,只要配置 git 使用 Windows 的 SSH 就行了

配置

一行代码搞定

powershell 复制代码
git config --global core.sshCommand "'C:\Windows\System32\OpenSSH\ssh.exe'"

有问题?

一开始我配置完了运行老是出错

复制代码
CreateProcessW failed error:2
posix_spawnp: No such file or directory

后面加了 -v 参数才发现是我的 ~/.ssh/config 里的配置

之前配置了 GitHub 走代理

复制代码
Host github.com
    User git
    Port 443
    HostName ssh.github.com
    TCPKeepAlive yes
    ProxyCommand "$HOME/scoop/apps/git/current/mingw64/bin/connect.exe -S 127.0.0.1:7890 -a none %h %p"

之前在 git 自带的 ssh 环境下运行没问题,但现在用了 Windows 的 ssh 反而不行了,于是把这个 ProxyCommand 配置注释掉,就 OK 了

如果有遇到类似问题的同学,可以参考一下。

参考资料

相关推荐
vistaup4 小时前
windows git 更新当前目录下所有的文件(非递归)
windows·git
王码码20355 小时前
Flutter for OpenHarmony:Flutter 三方库 algoliasearch 毫秒级云端搜索体验(云原生搜索引擎)
android·前端·git·flutter·搜索引擎·云原生·harmonyos
HelloTonyGo6 小时前
个人游戏笔记本免费“养龙虾”(Win10+WSL2+OpenClaw 部署与配置指南)
windows·ubuntu·wsl2·openclaw
待续3016 小时前
OpenClaw 安装及使用教程(Windows / macOS / Linux)
linux·windows·macos
sthnyph8 小时前
MySQL 8.0在windows环境安装及配置
windows·mysql·adb
Irene19918 小时前
Git 命令汇总表(基于一次完整的 Git 实战经验整理,涵盖从安装配置到日常开发、问题排查的所有常用命令)
git·常用命令
深念Y9 小时前
Windows 11 23H2 搜狗输入法卸载后任务栏严重卡顿问题分析
windows
q5431470879 小时前
Redis Desktop Manager(Redis可视化工具)安装及使用详细教程
redis·git·bootstrap
素雨迁喜11 小时前
Linux平台下git工具的使用
linux·运维·git
AI-小柒11 小时前
DataEyes 聚合平台 + Claude Code Max 编程实战
android·开发语言·人工智能·windows·python·macos·adb