Kanji
・云架构工程师 / 自由职业 ・1993年出生 ・爱媛县出身 / 现居东京都涩谷区 ・5年 AWS 实战经验 个人资料详情
目录
gh auth switch
gh auth setup-git
~/.gitconfig
includeIf
GH_TOKEN
git push
Permission denied
personal
work
yq
!gh auth git-credential
当我在 2023 年 2 月第一次写这篇文章时,GitHub CLI( gh )还没有提供保存和切换多个账号的官方功能,当时只能通过自定义别名备份并覆盖 ~/.config/gh/hosts.yml 文件来变通解决。
gh
~/.config/gh/hosts.yml
从 GitHub CLI v2.40.0(2023 年 12 月发布) 开始,官方正式推出了原生多账号切换命令 gh auth switch 。现在(2026 年,基于 gh version 2.102.0 验证),无需手动修改配置文件,只需以下标准命令即可在多个 GitHub 账号(如个人账号与公司工作账号)之间秒级切换:
gh version 2.102.0
# 1. 追加登录第 2 个或更多账号(不会覆盖已有账号,而是并存保留) gh auth login # 2. 查看当前已登录的所有账号列表及激活(Active)状态 gh auth status # 3. 切换账号(若只有 2 个账号,不带参数即可直接来回切换) gh auth switch # 4. 指定用户名进行非交互式一键切换(适合脚本或 3 个以上账号场景) gh auth switch -u kanji-shinohara # 5. 将 Git (git push / pull / clone) 的 HTTPS 认证与 GitHub CLI 当前激活账号绑定 gh auth setup-git
如果你只需要切换 gh pr create 或 gh issue list 的执行账号,使用上面的 gh auth switch 就足够了。
gh pr create
gh issue list
但在一台 Mac 上每天同时开发“公司业务仓库”和“个人开源仓库”时,我多次踩到仅靠 gh auth switch 无法解决的 3 个实战坑点 :
git commit
Author
user.name
user.email
GITHUB_TOKEN
本文将从官方 gh auth switch 的标准用法讲起,结合双层隔离架构彻底解决上述 3 个痛点,并分享我目前在生产环境中使用的角色别名配置。
▲ GitHub CLI (gh auth switch) API/HTTPS 认证切换与 Git includeIf 提交者隔离双层架构全景图
gh auth login
在已登录第一个账号的状态下,直接再次执行 gh auth login 并完成第二个账号的授权:
gh auth login -h github.com -p https -w
新版 GitHub CLI 默认将 OAuth 令牌加密保存在操作系统的安全密钥环中(macOS Keychain、Windows Credential Manager、Linux Secret Service),不再将明文 oauth_token 写入 ~/.config/gh/hosts.yml 。
oauth_token
gh auth status
$ gh auth status github.com ✓ Logged in to github.com account kanji-shinohara (keyring) - Active account: true - Git operations protocol: https - Token: gho_************************************ - Token scopes: 'gist', 'read:org', 'repo', 'workflow' ✓ Logged in to github.com account work-taro (keyring) - Active account: false - Git operations protocol: https - Token: gho_************************************ - Token scopes: 'gist', 'read:org', 'repo', 'workflow'
在脚本中如果只需检查当前激活的账号,可以使用:
# 仅显示当前激活的账号状态 gh auth status --active # 仅输出当前激活的 GitHub 用户名 gh api user --jq '.login'
# 方式 A:只有 2 个账号时直接交替切换 gh auth switch # 方式 B:显式指定用户名切换(推荐用于脚本或 3 个以上账号) gh auth switch -u kanji-shinohara # 方式 C:指定 GitHub Enterprise Server (GHES) 主机与用户名切换 gh auth switch --hostname github.company.internal -u work-taro
git clone
这是因为 Git 自身的凭据助手(例如 macOS 的 osxkeychain )缓存了旧账号的凭据,在 GitHub CLI 之前拦截了 HTTPS 认证。
osxkeychain
gh auth setup-git # 清空 github.com 的系统默认凭据助手,强制仅使用 gh auth git-credential git config --global --replace-all 'credential.https://github.com.helper' '' git config --global --add 'credential.https://github.com.helper' '!gh auth git-credential'
此时 ~/.gitconfig 中会写入以下配置,确保 gh auth switch 切换后 git push 立即同步生效:
[credential "https://github.com"] helper = helper = !/opt/homebrew/bin/gh auth git-credential
gh auth switch 仅切换 API 与 HTTPS 通信的认证令牌,绝对不会修改 Git 提交记录中的 user.name 和 user.email 。
为了防止把公司邮箱提交到个人开源仓库,我使用 ~/.gitconfig 的 includeIf 按工作目录( ~/workspace/personal/ 与 ~/workspace/work/ )自动切换 Git 提交身份:
~/workspace/personal/
~/workspace/work/
# ~/.gitconfig [user] name = Kanji Shinohara email = kanji.shinohara@example.com # 个人开发目录下的所有仓库自动应用个人邮箱 [includeIf "gitdir:~/workspace/personal/"] path = ~/.gitconfig-personal # 工作项目目录下的所有仓库自动应用公司邮箱 [includeIf "gitdir:~/workspace/work/"] path = ~/.gitconfig-work [credential "https://github.com"] helper = helper = !/opt/homebrew/bin/gh auth git-credential
# ~/.gitconfig-personal [user] name = Kanji Shinohara email = kanji.shinohara@gmail.com
# ~/.gitconfig-work [user] name = Kanji Shinohara email = kanji.shinohara@company.co.jp
为了在 AI 编程代理(Claude Code、Antigravity)的全局规则文件或自动化脚本中统一使用 gh switch-profile personal 而不是具体用户名,可以在 ~/.config/gh/config.yml 中配置以下轻量级别名:
gh switch-profile personal
~/.config/gh/config.yml
# ~/.config/gh/config.yml git_protocol: https aliases: switch-profile: >- !case "$1" in personal) gh auth switch -h github.com -u kanji-shinohara ;; work) gh auth switch -h github.com -u work-taro ;; *) echo "[ERROR] Usage: gh switch-profile <personal|work>" && exit 1 ;; esac && gh auth status --active
gh switch-profile personal && git push origin HEAD
hosts.yml
-u <user>
人工甘味料や添加物のNG除外と有効成分あたりの実質単価比較データベース。
提供逼真 AI 模拟通话与多维反馈,结合 16 讲实战社交学院与工作室级 AI 肖像生成,全面提升社交互动质量。
拍小票即可完成高精度智能拆单,支持 LINE 登录无缝结算团队聚会支出,告别繁琐账单分摊。
占卜师、管家、调酒师等 AI 角色实时引导话题与聚会游戏,让线下与线上聚餐氛围瞬间升温。
Real-time AI quant analysis of corporate earnings reports.