A kubectx-style context switcher for GitHub CLI - manage multiple GitHub accounts easily. This idea was based on https://github.com/automationpi/gh-context then customized a bit to my liking. Since I wanted to keep the convenient gh context subcommand, I had to name it the repo gh-context.
When you switch contexts, gh-context:
- Sets the active context
- Updates
~/.ssh/configto use the correct SSH key for that account - Switches the
ghCLI authentication to the correct user
This means both git push and gh commands will use the right credentials automatically.
gh extension install peterjmorgan/gh-contextBefore using gh-context, set up your ~/.ssh/config with your different SSH keys:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_work
# IdentityFile ~/.ssh/id_personal
Key points:
- All IdentityFile lines for github.com should be in a single
Host github.comblock - Comment out the keys you're not currently using with
# gh-contextwill uncomment/comment these lines when switching contexts
# Create a context from your current session (auto-detects active SSH key)
gh context new --from-current --name work
# Create a context with explicit SSH key
gh context new --from-current --name personal --ssh-key ~/.ssh/id_personal
# Switch contexts (updates SSH config + gh auth)
gh context use personal
# Verify everything is set up
gh context auth-status| Command | Description |
|---|---|
list |
List all contexts with active indicator |
current |
Show active context and repo-bound context |
new |
Create a new context |
use <name> |
Switch to a context (updates SSH config + gh auth) |
delete <name> |
Remove a saved context |
bind <name> |
Bind current repository to a context |
unbind |
Remove repository binding |
apply |
Apply the repo's bound context |
shell-hook [shell] |
Print shell integration code |
auth-status |
Show authentication status for all contexts |
# Auto-detect user and SSH key from current state
gh context new --from-current --name work
# Override the SSH key
gh context new --from-current --name personal --ssh-key ~/.ssh/id_personalgh context new \
--hostname github.com \
--user myusername \
--ssh-key ~/.ssh/id_mykey \
--name mycontextWhen you run gh context use personal, the tool:
- Finds the
Host github.comblock in~/.ssh/config - Comments out all
IdentityFilelines - Uncomments the
IdentityFileline matching your context's SSH key - Creates a backup at
~/.ssh/config.bak
Before:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_work
# IdentityFile ~/.ssh/id_personal
After gh context use personal:
Host github.com
HostName github.com
User git
# IdentityFile ~/.ssh/id_work
IdentityFile ~/.ssh/id_personal
Bind repositories to contexts for automatic switching:
# In your work repo
cd ~/work/project
gh context bind work
# In your personal repo
cd ~/personal/project
gh context bind personalAdd automatic context switching when entering repositories:
gh context shell-hook bash >> ~/.bashrc
source ~/.bashrcgh context shell-hook zsh >> ~/.zshrc
source ~/.zshrcgh context shell-hook powershell >> $PROFILE
. $PROFILEgh context shell-hook fish >> ~/.config/fish/config.fish
source ~/.config/fish/config.fishContexts are stored in ~/.config/gh/contexts/ (or %APPDATA%\gh\contexts on Windows):
HOSTNAME=github.com
USER=myuser
TRANSPORT=ssh
SSH_KEY=~/.ssh/id_personal
# 1. Set up SSH keys (if not already done)
ssh-keygen -t ed25519 -f ~/.ssh/id_work -C "work@company.com"
ssh-keygen -t ed25519 -f ~/.ssh/id_personal -C "personal@gmail.com"
# 2. Add keys to GitHub accounts (via GitHub web UI)
# 3. Set up ~/.ssh/config
cat >> ~/.ssh/config << 'EOF'
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_work
# IdentityFile ~/.ssh/id_personal
EOF
# 4. Log in to both accounts with gh
gh auth login # for work account
gh auth login # for personal account (add second account)
# 5. Create contexts
gh context new --from-current --name work --ssh-key ~/.ssh/id_work
gh context new --hostname github.com --user personal-username --ssh-key ~/.ssh/id_personal --name personal
# 6. Switch and verify
gh context use personal
gh context auth-status
git push # Now uses personal SSH keyMake sure your ~/.ssh/config has a Host github.com block with the IdentityFile lines:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_work
# IdentityFile ~/.ssh/id_personal
- Check
~/.ssh/configwas updated:cat ~/.ssh/config - Verify backup exists:
ls -la ~/.ssh/config.bak - Run
gh context auth-statusto see current state
- Run
gh context auth-statusto check both GH Auth and SSH Active status - Make sure both show ✅ for the context you want to use
go build -o gh-context .MIT