Kanji
・클라우드 엔지니어 / 프리랜서 ・1993년생 ・에히메현 출신 / 도쿄도 시부야구 거주 ・AWS 경력 5년 프로필 상세
목차
gh auth switch
gh auth setup-git
~/.gitconfig
includeIf
GH_TOKEN
git push
Permission denied
personal
work
yq
!gh auth git-credential
제가 이 글을 처음 작성했던 2023년 2월 당시에는 GitHub CLI( gh )에 여러 계정을 유지하고 전환하는 공식 기능이 없었기 때문에, ~/.config/gh/hosts.yml 파일을 복사해 덮어쓰는 자체 별칭(alias)으로 해결해야 했습니다.
gh
~/.config/gh/hosts.yml
이후 GitHub CLI v2.40.0(2023년 12월 릴리스) 에서 공식 다중 계정 전환 명령어인 gh auth switch 가 도입되었습니다. 현재(2026년 기준, gh version 2.102.0 검증 완료)는 설정 파일을 직접 수정하지 않고 아래 표준 명령어만으로 개인용과 업무용 GitHub 계정을 즉시 전환할 수 있습니다.
gh version 2.102.0
# 1. 두 번째 이후 계정 추가 로그인 (기존 계정을 덮어쓰지 않고 함께 보관됨) gh auth login # 2. 현재 로그인된 모든 계정 목록과 활성(Active) 상태 확인 gh auth status # 3. 계정 전환 (등록된 계정이 2개뿐이라면 인자 없이 즉시 토글 전환) gh auth switch # 4. 계정명을 지정하여 비대화형으로 즉시 전환 (스크립트 및 3개 이상 계정 환경) gh auth switch -u kanji-shinohara # 5. Git (git push / pull / clone) HTTPS 인증에도 GitHub CLI 전환 상태를 연동 gh auth setup-git
단순히 gh pr create 나 gh issue list 의 실행 계정만 바꾸고 싶다면 위 gh auth switch 만으로 충분합니다.
gh pr create
gh issue list
하지만 실제로 한 대의 Mac에서 회사 업무용 저장소와 개인 개발 저장소를 매일 오가며 작업하다 보니, gh auth switch 만으로는 막을 수 없는 3가지 실무 함정 에 반복해서 빠졌습니다.
Author
user.name
user.email
GITHUB_TOKEN
이 글에서는 공식 gh auth switch 사용법부터 위 3가지 함정을 원천 차단하는 2계층 분리 설계, 그리고 제가 실제 환경에서 함께 사용하는 역할별 별칭 설정까지 정리해 소개합니다.
▲ GitHub CLI (gh auth switch) 기반 API/HTTPS 인증 전환과 Git includeIf 기반 Author 분리 전체 구조
gh auth login
첫 번째 계정으로 이미 로그인된 상태에서 그대로 gh auth login 을 다시 실행해 두 번째 계정을 인증합니다.
gh auth login -h github.com -p https -w
최신 GitHub CLI는 기본적으로 인증 토큰을 OS 보안 키링(macOS Keychain, Windows Credential Manager, Linux Secret Service)에 암호화하여 보관하므로 ~/.config/gh/hosts.yml 에 평문 토큰이 남지 않습니다.
gh auth status
$ gh auth status github.com ✓ Logged in to github.com account kanji-shinohara (keyring) - Active account: true - Git operations protocol: https - Token: gho_************************************ - Token scopes: 'gist', 'read:org', 'repo', 'workflow' ✓ Logged in to github.com account work-taro (keyring) - Active account: false - Git operations protocol: https - Token: gho_************************************ - Token scopes: 'gist', 'read:org', 'repo', 'workflow'
스크립트에서 현재 활성 계정만 확인하려면 다음 명령어를 사용합니다.
# 활성 계정만 표시 gh auth status --active # 현재 활성 GitHub 사용자명만 문자열로 추출 gh api user --jq '.login'
# 패턴 A: 등록된 계정이 2개인 경우 (실행할 때마다 교대로 토글 전환) gh auth switch # 패턴 B: 사용자명을 명시하여 전환 (스크립트 및 3개 이상 계정) gh auth switch -u kanji-shinohara # 패턴 C: GitHub Enterprise Server (GHES) 호스트 지정 전환 gh auth switch --hostname github.company.internal -u work-taro
git clone
Git 자체의 자격 증명 헬퍼(예: macOS의 osxkeychain )가 이전 계정의 캐시를 잡고 있어 GitHub CLI의 활성 계정 변경을 참조하지 않기 때문입니다.
osxkeychain
gh auth setup-git # github.com에 대한 기존 시스템 헬퍼를 초기화하고 gh auth git-credential만 사용하도록 설정 git config --global --replace-all 'credential.https://github.com.helper' '' git config --global --add 'credential.https://github.com.helper' '!gh auth git-credential'
이제 ~/.gitconfig 에 아래와 같이 설정되어 gh auth switch 즉시 git push 인증도 100% 동기화됩니다.
[credential "https://github.com"] helper = helper = !/opt/homebrew/bin/gh auth git-credential
gh auth switch 가 전환하는 것은 API 및 HTTPS 통신용 인증 토큰뿐이며, Git 커밋에 기록되는 user.name 과 user.email 은 전혀 변경되지 않습니다.
실수로 개인 저장소에 회사 이메일로 커밋하는 사고를 막기 위해, 작업 디렉터리( ~/workspace/personal/ 과 ~/workspace/work/ )에 따라 Git 설정을 자동 전환하는 includeIf 를 함께 설정합니다.
~/workspace/personal/
~/workspace/work/
# ~/.gitconfig [user] name = Kanji Shinohara email = kanji.shinohara@example.com # 개인 개발 디렉터리 하위에서는 개인용 설정 적용 [includeIf "gitdir:~/workspace/personal/"] path = ~/.gitconfig-personal # 업무용 디렉터리 하위에서는 업무용 설정 적용 [includeIf "gitdir:~/workspace/work/"] path = ~/.gitconfig-work [credential "https://github.com"] helper = helper = !/opt/homebrew/bin/gh auth git-credential
# ~/.gitconfig-personal [user] name = Kanji Shinohara email = kanji.shinohara@gmail.com
# ~/.gitconfig-work [user] name = Kanji Shinohara email = kanji.shinohara@company.co.jp
AI 코딩 에이전트(Claude Code, Antigravity 등)의 공통 규칙 파일이나 자동화 스크립트에서 사용자명 대신 gh switch-profile personal 처럼 역할명으로 호출하고 싶다면 ~/.config/gh/config.yml 에 다음 별칭을 등록합니다.
gh switch-profile personal
~/.config/gh/config.yml
# ~/.config/gh/config.yml git_protocol: https aliases: switch-profile: >- !case "$1" in personal) gh auth switch -h github.com -u kanji-shinohara ;; work) gh auth switch -h github.com -u work-taro ;; *) echo "[ERROR] Usage: gh switch-profile <personal|work>" && exit 1 ;; esac && gh auth status --active
gh switch-profile personal && git push origin HEAD
hosts.yml
-u <user>
人工甘味料や添加物のNG除外と有効成分あたりの実質単価比較データベース。
실전 같은 AI 음성 모의 통화와 5축 정밀 피드백. 16강 인터랙티브 강의와 스튜디오급 AI 프로필 생성 지원.
영수증 촬영 즉시 Gemini AI가 품목과 금액을 자동 인식. 모임 및 여행 경비를 손쉽게 더치페이하고 정산합니다.
점술사, 집사, 바텐더 AI 캐릭터가 직접 게임을 진행하고 대화 주제를 던져 모임을 흥미진진하게 만듭니다.
Real-time AI quant analysis of corporate earnings reports.