GitHub CLI Multi-Account Auto-Switcher: Zero-Config Solution
If you juggle work and personal GitHub accounts like I do, constantly checking which account is active before running gh pr create
gets old fast. Here's a perfect solution that completely eliminates this friction.
The Problem
Working with multiple GitHub accounts means:
- Forgetting which account is currently active
- Getting "No default remote repository" errors
- Manually running
gh auth switch
andgh repo set-default
- Accidentally creating PRs with the wrong account
The Solution
Create a gh
wrapper script that automatically detects the current repository's owner, switches to the correct GitHub account, and sets the default repository before executing any command.
Implementation
#!/bin/bash
# Path to original gh binary
ORIGINAL_GH="/usr/local/bin/gh" # Adjust for your system
# Get current repository's remote URL
remote_url=$(git remote get-url origin 2>/dev/null)
# If not in a git repo, pass through to original gh
if [ $? -ne 0 ]; then
exec "$ORIGINAL_GH" "$@"
fi
# Extract full repository name (owner/repo) - remove .git suffix
if [[ $remote_url =~ github\.com[:/]([^/]+/[^/]+) ]]; then
repo_full_name="${BASH_REMATCH[1]}"
repo_full_name="${repo_full_name%.git}"
repo_owner=$(echo "$repo_full_name" | cut -d'/' -f1)
else
exec "$ORIGINAL_GH" "$@"
fi
# Get current active GitHub account
current_user=$("$ORIGINAL_GH" api user --jq '.login' 2>/dev/null)
if [ $? -ne 0 ]; then
exec "$ORIGINAL_GH" "$@"
fi
# Switch account if needed
if [ "$repo_owner" != "$current_user" ]; then
echo "→ Repository belongs to $repo_owner, switching from $current_user..." >&2
"$ORIGINAL_GH" auth switch >/dev/null 2>&1
fi
# Set default repository to avoid "No default remote repository" errors
"$ORIGINAL_GH" repo set-default "$repo_full_name" >/dev/null 2>&1
# Execute original command
exec "$ORIGINAL_GH" "$@"
Setup
- Find your original gh path:
which gh
# Use this path in the ORIGINAL_GH variable
- Create the wrapper script:
# Save script as ~/.local/bin/gh
chmod +x ~/.local/bin/gh
- Adjust PATH priority:
# Add to ~/.bashrc or ~/.zshrc
export PATH="$HOME/.local/bin:$PATH"
- Verify setup:
source ~/.bashrc
which gh # Should show ~/.local/bin/gh
Usage
All GitHub CLI commands now automatically use the correct account:
# In personal repository
gh pr create # Uses personal account, sets correct default repo
# In work repository
gh pr create # Uses work account, sets correct default repo
# All other commands work transparently
gh issue list
gh pr view
gh repo clone username/repo
Advanced: Working with Forks
For forked repositories where you want to view PRs in the upstream repo:
# View your PRs in the upstream repository
gh pr list --author @me --repo upstream-owner/repo-name
# Or temporarily switch to upstream
gh repo set-default upstream-owner/repo-name
gh pr view [PR_NUMBER]
How It Works
- Extracts repository owner from the current directory's
origin
remote URL - Compares repository owner with currently active GitHub account
- Automatically runs
gh auth switch
if accounts don't match - Always sets the correct default repository to prevent CLI errors
- Transparently proxies all other
gh
commands
Benefits
- Zero configuration required - works out of the box
- Zero habit changes needed - still use
gh
commands normally - Eliminates common errors - no more "No default remote repository" messages
- Multi-account friction eliminated - never think about which account is active again
Perfect for developers who work across multiple GitHub organizations or maintain both work and personal projects.