Windows 上发布内容到 GitHub(代理环境实战)
在 Windows(国内网络/Clash 代理环境)上把文件或技能发布到 GitHub 的完整流程。 已实战验证:解决 GitHub 访问、安装 gh、设备流登录、创建仓库、API 上传、公开化。
核心要点
- GitHub 访问不通的根因通常是代理没运行:检查 Clash 内核端口(默认 7897)是否 LISTENING + 系统代理 ProxyEnable 是否=1。
- gh 的 HTTP 请求不读 Windows 系统代理,只读环境变量:运行 gh 前必须
export HTTPS_PROXY=http://127.0.0.1:7897。 - git push 走代理可能 SSL 失败(schannel/openssl 都不行、Clash 分流 github.com 不稳)→ 改用 gh api 上传文件(api.github.com 走代理稳定)。
- gh 交互式登录在 pty 后台的坑:卡在 "Press Enter to open..." 时,
process submit传空数据无效,必须用process write发\r(Windows 回车)。 - git 直连 github.com 有时通有时超时(DNS 轮询到不同 IP),curl/PowerShell 测试结果可能矛盾,以 PowerShell Invoke-WebRequest 为准。
标准流程
1. 检查并修复网络(GitHub 访问不了时)
# 检查代理端口
netstat -ano | grep -E "7897" # 无输出 = Clash 内核没跑
# 检查系统代理
powershell -NoProfile -Command "Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' | Select ProxyEnable,ProxyServer"
# 干净网络测试(PowerShell 为准,别信 git-bash curl 的偶然 200)
powershell -NoProfile -Command "Invoke-WebRequest -Uri 'https://github.com' -UseBasicParsing -TimeoutSec 10 | Select StatusCode"
修复:启动 Clash Verge(Start-Process 'D:\Clash Verge\clash-verge.exe',路径以实际为准),
等 6 秒确认 7897 LISTENING,然后开系统代理:
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyEnable -Value 1
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyServer -Value '127.0.0.1:7897'
# 广播变更(wininet.dll InternetSetOption, 选项 39 和 37)
2. 安装 gh CLI
winget install --id GitHub.cli --accept-source-agreements --accept-package-agreements --silent
export PATH="$PATH:/c/Program Files/GitHub CLI" # 新 shell 需手动加
3. gh 设备流登录(浏览器授权)
export HTTPS_PROXY="http://127.0.0.1:7897" ; export HTTP_PROXY="http://127.0.0.1:7897" ; export ALL_PROXY="http://127.0.0.1:7897"
gh auth login --hostname github.com --git-protocol https --web
用 background + pty 启动,然后:
- 遇到 "Authenticate Git with your GitHub credentials?" →
process submit发Y - 拿到 "one-time code: XXXX-XXXX" → 用 Edge 打开 https://github.com/login/device(`Start-Process msedge 'https://github.com/login/device'`),让用户登录账户并输入 code 授权
- 卡在 "Press Enter to open..." 时用
process write发\r(submit 空数据无效) - 授权后轮询成功输出 "✓ Authentication complete.",验证:
gh auth status # 应显示 Logged in to github.com account <用户名>
4. 创建仓库
gh repo create <repo-name> --private --source . --push --description "..."
# 或先建空仓库再上传:
gh repo create <repo-name> --private --description "..."
5. 上传文件(git push SSL 失败时的可靠替代)
# 方案 A:git(代理下可能失败,失败见方案 B)
git config http.proxy http://127.0.0.1:7897
git push -u origin main
# 方案 B(推荐,稳定):gh api 上传,内容 base64
cd <repo-dir>
gh api --method PUT "repos/<owner>/<repo>/contents/<path>" \
-f message="Add <path>" \
-f content="$(base64 -w0 <本地文件>)" --jq '.content.html_url'
注意:文件路径含目录(如 autonomous-ai-agents/xxx/SKILL.md)直接作为 contents/ 参数即可,自动建目录。
6. 公开化 + 完善
# 公开必须带后果确认标志,否则报错
gh repo edit <owner>/<repo> --visibility public --accept-visibility-change-consequences
# Topics
gh repo edit <owner>/<repo> --add-topic "topic1,topic2"
# LICENSE(MIT 模板 + 上传)
gh api --method PUT "repos/<owner>/<repo>/contents/LICENSE" -f message="Add MIT License" -f content="$(base64 -w0 LICENSE)"
# 验证
gh repo view <owner>/<repo> --json visibility,url
陷阱清单
- git-bash 转义:PowerShell 命令在 bash 里执行时
$写\$,内部双引号写\"。 - gh repo create --push 失败:仓库已创建但 push 失败(SSL),远程已存在;后续直接用 gh api 上传即可,不用重建。
- 一次性验证码有效期:约 15 分钟,过期需重新 gh auth login。
--visibility报错:必须加--accept-visibility-change-consequences。- gh token 获取:
gh auth token可拿到 token(供脚本用),注意别打印到日志。 - hermes .env 里的 GITHUB_TOKEN 可能是注释占位符(
# GITHUB_TOKEN=ghp_xx...),不是有效凭据,别浪费时间用它。 - Edge 打开 github.com/login/device 会累积标签页:每次 Start-Process 新开标签,操作前先清理或指定当前标签。
验证步骤
gh auth status显示已登录账户gh repo view <owner>/<repo>显示正确可见性gh api repos/<owner>/<repo>/contents列出所有文件- 浏览器打开仓库 URL 确认内容可访问