Kanji
・ Cloud engineer / freelance ・ Born in 1993 ・ Born in Ehime Prefecture / Lives in Shibuya-ku, Tokyo ・ AWS history 5 years Profile details
gh auth login
Table of Contents
I found the method using GitHub CLI aliases described in GH CLI multi-account switch to be simple, so I customized it a bit for my own use. The referenced site required you to manually append ~/.config/gh/hosts.yml.${profile_name} to ~/.config/gh/config.yml for each GitHub account you want to access.
~/.config/gh/hosts.yml.${profile_name}
~/.config/gh/config.yml
With the method below, you can create and switch profiles using aliases, so you don’t need to directly edit the ~/.config/gh/config.yml file except during the initial setup.
Note: This method only switches authentication for the GitHub CLI, not for Git itself. For Git, you can set the credential helper as shown below, so you don’t need to switch accounts in the GitHub CLI. (Update 2023/02/21) Even with the settings below, you still need to switch authentication in the GitHub CLI to access repositories.
git config --global 'credential.https://github.com.helper' '' git config --global --add 'credential.https://github.com.helper' '!gh auth git-credential'
For the initial setup, open the ~/.config/gh/config.yml file in an editor like vi and add the following aliases under the aliases key: create-profile , switch-profile , delete-profile , and list-profiles . You can rename or modify these aliases as you like. - create-profile : Checks your current authentication status with gh auth status and interactively asks if you want to create a profile. If yes, it copies ~/.config/gh/hosts.yml to ~/.config/gh/hosts.yml.${profile_name} . - switch-profile : Checks if ~/.config/gh/hosts.yml.${profile_name} exists. If it does, it copies it to ~/.config/gh/hosts.yml . - delete-profile : Interactively asks if you want to delete a profile, and if yes, deletes ~/.config/gh/hosts.yml.${profile_name} . - list-profiles : Lists all current profiles.
aliases
create-profile
switch-profile
delete-profile
list-profiles
gh auth status
~/.config/gh/hosts.yml
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|^| - |"'
yq
# 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
After logging in to your GitHub account with gh auth login , run the following command to create a new profile:
gh create-profile ${profile_name}
Example output: The script will ask for confirmation before creating a new profile to prevent accidental overwrites. Enter y and press Enter to create the profile file.
y
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
To switch between GitHub accounts, run:
gh switch-profile ${profile_name}
Example output: The alias simply checks if the profile file exists and switches if it does.
# 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}
To delete a profile, run:
gh delete-profile ${profile_name}
Example output: The script will ask for confirmation before deleting the profile. Enter y and press Enter to delete the profile. This does not log you out of the current session.
Do you want to delete this profile? (y/N): y Hosts file deleted: ~/.config/gh/hosts.yml.xxxx
To list all profiles, run:
gh list-profiles
Example output: Only the profile names are displayed.
- xxxx - yyyy - zzzz
I personally use Bitwarden instead of 1Password, so I haven’t tried this myself, but it seems you can also switch GitHub accounts using 1Password.