macOS 上让 agent 能非交互 git push
症状
fatal: could not read Username for 'https://github.com': terminal prompts disabled
诊断顺序(别跳步)
git config --get credential.helper→ 通常是osxkeychain- 条目到底有没有:
printf "protocol=https\nhost=github.com\n\n" | git credential-osxkeychain get- 返回空 = 钥匙串确实没有条目(不是沙箱拦截——若几百毫秒内正常结束就是真没有)
ls -la ~/.ssh→ 没有密钥就别试 SSH 路线了,直接走 HTTPS + PAT
关键坑:git 自带的 osxkeychain 助手在 agent 环境里写不进去
实测(2026-09-13):git credential-osxkeychain store——带 username 和不带、
沙箱内与沙箱外(提权)都试过,全都不落盘:
security find-internet-password -s github.com 查不到任何条目,get 也读不出来。
但直接用 security CLI 就能正常读写钥匙串:
security add-generic-password / find-generic-password 都成功。
→ 结论:不要在 git 自带助手上浪费时间,直接改用 security。
可用做法
# 1. 存入钥匙串(-U:已存在则更新)
security add-generic-password -s github.com -a <GitHub用户名> -w <TOKEN> -U
# 2. 让 git 用 security 读(写在仓库本地配置,侵入最小)
git config --local credential.helper \
'!f() { echo username=<GitHub用户名>; echo "password=$(security find-generic-password -s github.com -a <GitHub用户名> -w 2>/dev/null)"; }; f'
# 3. 验证(无内容可推时应输出 Everything up-to-date)
git push origin main
验证通过后,普通(沙箱内)命令也能用,不需要每次提权。
若要让所有仓库共用,用 git config --global --add credential.helper '...'(--add 保留原有 osxkeychain 作为第一顺位)。
备选(钥匙串确实不可用时)
git config --local credential.helper 'store --file=~/.git-credentials'
再写入 https://<user>:<token>@github.com。明文落盘(chmod 600),
必须用户明确同意,并提醒用完 revoke。
前置与礼貌
- token 只能由用户提供:GitHub 已不支持账号密码;fine-grained PAT 需给目标仓库
Contents: Read and write - 推送前先
git status确认干净、git log --oneline origin/main..HEAD确认要推什么、并问一句是否要--tags - 用户把 PAT 贴进对话会留痕 → 主动告诉他去 https://github.com/settings/tokens 决定是否 revoke
- 不要把 token 写进任何配置文件、memory 或 skill