Kanji
・雲端架構工程師 / 自由職業 ・1993年出生 ・愛媛縣出身 / 現居東京都澀谷區 ・5年 AWS 實戰經驗 個人檔案詳情
目錄
我發現 GH CLI多賬戶切換 中描述的使用GitHub CLI別名的方法很簡單,所以我對其進行了一些定製以供自己使用。引用的站點要求您為要訪問的每個 GitHub 帳戶手動將 ~/.config/gh/hosts.yml.${profile_name} 附加到 ~/.config/gh/config.yml 。
~/.config/gh/hosts.yml.${profile_name}
~/.config/gh/config.yml
透過下面的方法,您可以使用別名建立和切換配置檔案,因此除了初始設定期間之外,您不需要直接編輯 ~/.config/gh/config.yml 檔案。
注意:此方法僅切換 GitHub CLI 的身份驗證,而不是 Git 本身的身份驗證。 對於Git,您可以如下所示設定憑證助手,這樣您就不需要在GitHub CLI中切換帳戶了 (2023/02/21 更新)即使進行了以下設定,您仍然需要在 GitHub CLI 中切換身份驗證才能訪問儲存庫。
git config --global 'credential.https://github.com.helper' '' git config --global --add 'credential.https://github.com.helper' '!gh auth git-credential'
對於初始設定,請在 vi 等編輯器中開啟“~/.config/gh/config.yml”檔案,並在“aliases”鍵下新增以下別名:“create-profile”、“switch-profile”、“delete-profile”和“list-profiles”。您可以根據需要重新命名或修改這些別名。 - create-profile :使用 gh auth status 檢查您當前的身份驗證狀態,並互動詢問您是否要建立配置檔案。如果是,它將 ~/.config/gh/hosts.yml 複製到 ~/.config/gh/hosts.yml.${profile_name} 。 - switch-profile :檢查 ~/.config/gh/hosts.yml.${profile_name} 是否存在。如果是,它將其複製到“~/.config/gh/hosts.yml”。 - delete-profile :互動式詢問您是否要刪除配置檔案,如果是,則刪除 ~/.config/gh/hosts.yml.${profile_name} 。 - list-profiles :列出所有當前配置檔案。
create-profile
gh auth status
~/.config/gh/hosts.yml
switch-profile
delete-profile
list-profiles
git_protocol: https aliases: create-profile: '![ -z "$1" ] && echo "[ERROR] Please specify the profile name." && exit 1; gh auth status && read -p "Do you want to create this profile? (y/N): " yn; case "$yn" in [yY]*) cp ~/.config/gh/hosts.yml ~/.config/gh/hosts.yml.$1 && echo "New hosts file created: ~/.config/gh/hosts.yml.$1";; esac' switch-profile: '![ -z "$1" ] && echo "[ERROR] Please specify the profile name." && exit 1; if [ -e ~/.config/gh/hosts.yml.$1 ]; then cp ~/.config/gh/hosts.yml.$1 ~/.config/gh/hosts.yml && gh auth status && echo "[INFO] Hosts file switched"; else echo "[ERROR] Hosts file not found: ~/.config/gh/hosts.yml.$1"; fi' delete-profile: '![ -z "$1" ] && echo "[ERROR] Please specify the profile name." && exit 1; read -p "Do you want to delete this profile? (y/N): " yn; case "$yn" in [yY]*) rm -f ~/.config/gh/hosts.yml.$1 && echo "Hosts file deleted: ~/.config/gh/hosts.yml.$1";; esac' list-profiles: '!ls ~/.config/gh/hosts.yml.* | sed -e "s|^.*hosts.yml.||" | sed -e "s|^| - |"'
# 如何使用命令切換GitHub CLI命令的帳戶 yq -o yaml -i '.aliases += { "create-profile": "''''![ -z \"$1\" ] && echo \"[ERROR] Please specify the profile name.\" && exit 1; gh auth status && read -p \"Do you want to create this profile? (y/N): \" yn; case \"$yn\" in [yY]*) cp ~/.config/gh/hosts.yml ~/.config/gh/hosts.yml.$1 && echo \"New hosts file created: ~/.config/gh/hosts.yml.$1\";; esac''''" } ' ~/.config/gh/config.yml # Switch profile alias yq -o yaml -i '.aliases += { "switch-profile": "''''![ -z \"$1\" ] && echo \"[ERROR] Please specify the profile name.\" && exit 1; if [ -e ~/.config/gh/hosts.yml.$1 ]; then cp ~/.config/gh/hosts.yml.$1 ~/.config/gh/hosts.yml && gh auth status && echo \"Hosts file switched\"; else echo \"[ERROR] Hosts file not found: ~/.config/gh/hosts.yml.$1\"; fi''''" }' ~/.config/gh/config.yml # Delete profile alias yq -o yaml -i '.aliases += { "delete-profile": "![ -z \"$1\" ] && echo \"[ERROR] Please specify the profile name.\" && exit 1; read -p \"Do you want to delete this profile? (y/N): \" yn; case \"$yn\" in [yY]*) rm -f ~/.config/gh/hosts.yml.$1 && echo \"Hosts file deleted: ~/.config/gh/hosts.yml.$1\";; esac" }' ~/.config/gh/config.yml # List profiles alias yq -o yaml -i '.aliases += { "list-profiles": "!ls ~/.config/gh/hosts.yml.* | sed -e \"s|^.*hosts.yml.||\" | sed -e \"s|^| - |\"" }' ~/.config/gh/config.yml
使用“gh auth login”登入 GitHub 帳戶後,執行以下命令建立新的配置檔案:
gh create-profile ${profile_name}
輸出示例:該指令碼將在建立新配置檔案之前要求確認,以防止意外覆蓋。輸入“y”並按 Enter 建立配置檔案。
github.com ✓ Logged in to github.com as xxxxxxxxxx (oauth_token) ✓ Git operations for github.com configured to use https protocol. ✓ Token: ******************* Do you want to create this profile? (y/N): y New hosts file created: ~/.config/gh/hosts.yml.xxxx
要在 GitHub 帳戶之間切換,請執行:
gh switch-profile ${profile_name}
示例輸出:別名只是檢查配置檔案是否存在,如果存在則進行切換。
# If the profile exists github.com ✓ Logged in to github.com as xxxxxxxxxx (oauth_token) ✓ Git operations for github.com configured to use https protocol. ✓ Token: *******************c [INFO] Hosts file switched # If the profile does not exist [ERROR] Hosts file not found: ~/.config/gh/hosts.yml.test # If the profile exists but authentication fails github.com X Failed to log in to github.com account ${Git Hub Account Name} $(/Users/user01/.config/gh/hosts.yml) - Active account: true - The token in /Users/user01/.config/gh/hosts.yml is invalid. - To re-authenticate, run: gh auth login -h github.com - To forget about this account, run: gh auth logout -h github.com -u ${Git Hub Account Name}
要刪除配置檔案,請執行:
gh delete-profile ${profile_name}
輸出示例:指令碼將在刪除配置檔案之前要求確認。輸入“y”並按 Enter 鍵刪除配置檔案。這不會使您退出當前會話。
Do you want to delete this profile? (y/N): y Hosts file deleted: ~/.config/gh/hosts.yml.xxxx
要列出所有配置檔案,請執行:
gh list-profiles
輸出示例:僅顯示配置檔名稱。
- xxxx - yyyy - zzzz
我個人使用 Bitwarden 而不是 1Password,所以我自己沒有嘗試過,但似乎你也可以使用 1Password 切換 GitHub 帳戶。