Kanji
・Cloud Engineer / Freelance ・Geboren 1993 ・Aus Ehime / Wohnhaft in Shibuya, Tokio ・5 Jahre AWS-Erfahrung Profildetails
Inhaltsverzeichnis
Ich fand die in GH CLI-Multi-Account-Switch beschriebene Methode mit GitHub-CLI-Aliase einfach, also habe ich sie ein wenig für meinen eigenen Gebrauch angepasst.Auf der referenzierten Site mussten Sie „~/.config/gh/hosts.yml.${profile_name}“ für jedes GitHub-Konto, auf das Sie zugreifen möchten, manuell an „~/.config/gh/config.yml“ anhängen.
Mit der folgenden Methode können Sie Profile mithilfe von Aliasen erstellen und wechseln, sodass Sie die Datei „~/.config/gh/config.yml“ nur während der Ersteinrichtung direkt bearbeiten müssen.
Hinweis: Diese Methode schaltet nur die Authentifizierung für die GitHub-CLI um, nicht für Git selbst. Für Git können Sie den Anmeldeinformations-Helfer wie unten gezeigt festlegen, sodass Sie in der GitHub-CLI nicht das Konto wechseln müssen. (Update 21.02.2023) Auch mit den folgenden Einstellungen müssen Sie die Authentifizierung in der GitHub-CLI ändern, um auf Repositorys zugreifen zu können.
git config --global 'credential.https://github.com.helper' '' git config --global --add 'credential.https://github.com.helper' '!gh auth git-credential'
Öffnen Sie für die Ersteinrichtung die Datei „~/.config/gh/config.yml“ in einem Editor wie vi und fügen Sie die folgenden Aliase unter dem Schlüssel „aliases“ hinzu: „create-profile“, „switch-profile“, „delete-profile“ und „list-profiles“.Sie können diese Aliase nach Belieben umbenennen oder ändern. - „create-profile“: Überprüft Ihren aktuellen Authentifizierungsstatus mit „gh auth status“ und fragt interaktiv, ob Sie ein Profil erstellen möchten.Wenn ja, wird „~/.config/gh/hosts.yml“ nach „~/.config/gh/hosts.yml. p r o f i l e n a m e “ k o p i e r t . − „ s w i t c h − p r o f i l e “ : P r ü f t , o b „ /. c o n f i g / g h / h o s t s . y m l . {profile_name}“ existiert.Wenn dies der Fall ist, wird es nach „~/.config/gh/hosts.yml“ kopiert. - „delete-profile“: Fragt interaktiv, ob Sie ein Profil löschen möchten, und löscht, wenn ja, „~/.config/gh/hosts.yml.${profile_name}“. - „list-profiles“: Listet alle aktuellen Profile auf.
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
Nachdem Sie sich mit „gh auth login“ bei Ihrem GitHub-Konto angemeldet haben, führen Sie den folgenden Befehl aus, um ein neues Profil zu erstellen:
gh create-profile ${profile_name}
Beispielausgabe: Das Skript fragt vor dem Erstellen eines neuen Profils nach einer Bestätigung, um versehentliches Überschreiben zu verhindern.Geben Sie „y“ ein und drücken Sie die Eingabetaste, um die Profildatei zu erstellen.
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
Um zwischen GitHub-Konten zu wechseln, führen Sie Folgendes aus:
gh switch-profile ${profile_name}
Beispielausgabe: Der Alias prüft lediglich, ob die Profildatei vorhanden ist, und schaltet um, wenn dies der Fall ist.
# 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}
Um ein Profil zu löschen, führen Sie Folgendes aus:
gh delete-profile ${profile_name}
Beispielausgabe: Das Skript fragt vor dem Löschen des Profils nach einer Bestätigung.Geben Sie „y“ ein und drücken Sie die Eingabetaste, um das Profil zu löschen.Dadurch werden Sie nicht von der aktuellen Sitzung abgemeldet.
Do you want to delete this profile? (y/N): y Hosts file deleted: ~/.config/gh/hosts.yml.xxxx
Um alle Profile aufzulisten, führen Sie Folgendes aus:
gh list-profiles
Beispielausgabe: Es werden nur die Profilnamen angezeigt.
- xxxx - yyyy - zzzz
Ich persönlich verwende Bitwarden anstelle von 1Password, daher habe ich das noch nicht selbst ausprobiert, aber es scheint, dass Sie GitHub-Konten auch mit 1Password wechseln können.