Kanji
・클라우드 엔지니어 / 프리랜서 ・1993년생 ・에히메현 출신 / 도쿄도 시부야구 거주 ・AWS 경력 5년 프로필 상세
gh auth login
목차
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 파일을 직접 편집할 필요가 없습니다.
참고: 이 방법은 Git 자체가 아닌 GitHub CLI에 대한 인증만 전환합니다. Git의 경우 아래와 같이 Credential Helper를 설정할 수 있으므로 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 : 현재 프로필을 모두 나열합니다.
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
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
저는 개인적으로 1Password 대신 Bitwarden을 사용하기 때문에 직접 해보지는 않았지만, 1Password를 사용하면 GitHub 계정 전환도 가능한 것 같습니다.