Skip to content

Feature: Auto-switch accounts based on git config #12459

Description

@jayproulx

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:

  1. The config value exists
  2. 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:

  1. Check for git config github.account
  2. If found and user is authenticated with that account, use it
  3. 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementa request to improve CLI

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions