Summary
Add native support for automatic account switching based on git configuration, specifically reading git config github.account to determine which authenticated account to use for the current repository.
Motivation
Many developers manage multiple GitHub accounts (personal, work, different clients) and use git's conditional includes (includeIf) to automatically configure user identity per directory. However, gh CLI requires manual account switching with gh auth switch, which breaks the seamless multi-account workflow.
Current workflow (manual):
# Working on personal project
cd ~/projects/personal
gh auth switch --user personal-account
gh pr create
# Switching to work project
cd ~/projects/work
gh auth switch --user work-account
gh pr create
Desired workflow (automatic):
# Working on personal project
cd ~/projects/personal
gh pr create # Uses personal-account automatically
# Switching to work project
cd ~/projects/work
gh pr create # Uses work-account automatically
Proposed Solution
Enhance gh to read git config github.account and automatically use that account if:
- The config value exists
- The user has authenticated with that account
This mirrors how git itself handles multiple identities through conditional includes.
Example: Directory-based git configuration
# ~/.gitconfig
[includeIf "gitdir:~/projects/personal/"]
path = ~/.gitconfig-personal
[includeIf "gitdir:~/projects/work/"]
path = ~/.gitconfig-work
# ~/.gitconfig-personal
[user]
name = Personal Name
email = personal@example.com
[github]
account = personal-username
# ~/.gitconfig-work
[user]
name = Work Name
email = work@company.com
[github]
account = work-username
With this setup:
cd ~/projects/personal && git config github.account → returns personal-username
cd ~/projects/work && git config github.account → returns work-username
- Git automatically applies the correct config based on directory
gh would automatically use the corresponding authenticated account
Implementation
The logic would be:
- Check for
git config github.account
- If found and user is authenticated with that account, use it
- Otherwise, fall back to current behavior (active account)
This is backward compatible - it only activates when the config exists.
User Workaround
Users can currently implement this with a shell function wrapper:
gh() {
local gh_account=$(git config github.account 2>/dev/null)
if [[ -n "$gh_account" ]]; then
if ! command gh auth status 2>&1 | grep -q "$gh_account.*Active account: true"; then
command gh auth switch --user "$gh_account" &>/dev/null
fi
fi
command gh "$@"
}
However, native support would benefit all users managing multiple accounts.
Benefits
- Zero friction: Matches existing git multi-identity patterns
- Backward compatible: Only activates when config exists
- Proven pattern: Leverages git's established conditional include mechanism
- Low complexity: Simple config value lookup
- Better UX: Eliminates manual account switching
Related
This aligns with how other tools handle multi-account scenarios (e.g., AWS CLI with profiles, kubectl with contexts).
Summary
Add native support for automatic account switching based on git configuration, specifically reading
git config github.accountto determine which authenticated account to use for the current repository.Motivation
Many developers manage multiple GitHub accounts (personal, work, different clients) and use git's conditional includes (
includeIf) to automatically configure user identity per directory. However,ghCLI requires manual account switching withgh auth switch, which breaks the seamless multi-account workflow.Current workflow (manual):
Desired workflow (automatic):
Proposed Solution
Enhance
ghto readgit config github.accountand automatically use that account if:This mirrors how git itself handles multiple identities through conditional includes.
Example: Directory-based git configuration
With this setup:
cd ~/projects/personal && git config github.account→ returnspersonal-usernamecd ~/projects/work && git config github.account→ returnswork-usernameghwould automatically use the corresponding authenticated accountImplementation
The logic would be:
git config github.accountThis is backward compatible - it only activates when the config exists.
User Workaround
Users can currently implement this with a shell function wrapper:
However, native support would benefit all users managing multiple accounts.
Benefits
Related
This aligns with how other tools handle multi-account scenarios (e.g., AWS CLI with profiles, kubectl with contexts).