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>
提供逼真 AI 模擬通話與多維反饋,結合 16 講實戰社交學院與工作室級 AI 肖像生成,全面提升社交互動品質。
拍發票即可完成高精度智慧拆單,支援 LINE 登入無縫結算團隊聚會支出,告別繁雜帳單分攤。
占卜師、管家、調酒師等 AI 角色即時引導話題與聚會遊戲,讓線下與線上聚餐氛圍瞬間升溫。
Real-time AI quant analysis of corporate earnings reports.
人工甘味料や添加物のNG除外と有効成分あたりの実質単価比較データベース。
獲取本文介紹的完整設定檔包:完全相容 Antigravity、Kiro、Claude Code 和 Codex 的通用 AGENTS.md、hooks.json、Git 守衛指令碼及自動化驗證工具,即拷即用。