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|^| - |"'
# Create profile alias 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 帐户。