SSH Key Management
What it does
Section titled “What it does”gitswitch can force git to use a specific SSH key when you switch profiles.
gitswitch work# Now all git operations use ~/.ssh/id_workWhy you need this
Section titled “Why you need this”The problem
Section titled “The problem”SSH key management is complex:
- You have multiple SSH keys (personal, work, client A, client B)
- SSH agent tries all keys in order until one works
- Wrong key might work by accident (if your GitHub account has multiple authorized keys)
- Slow authentication if the agent tries many wrong keys first
- Confusion about which key is actually being used
The solution
Section titled “The solution”gitswitch forces a specific key per profile:
gitswitch add work "Alice" alice@company.com \ --ssh-key ~/.ssh/id_workWhen you switch to work, git uses only ~/.ssh/id_work. No guessing. No agent fallback.
How it works
Section titled “How it works”gitswitch sets:
git config --global core.sshCommand \ "ssh -i ~/.ssh/id_work -o IdentitiesOnly=yes"The flags mean:
-i <key>— Use this specific key-o IdentitiesOnly=yes— Don’t try other keys from the agent
Git respects this setting and uses only that key for all operations.
Add profile with SSH key
Section titled “Add profile with SSH key”gitswitch add work "Alice" alice@company.com \ --ssh-key ~/.ssh/id_workGenerate SSH keys (if you don’t have them)
Section titled “Generate SSH keys (if you don’t have them)”# Generate a new keyssh-keygen -t ed25519 -f ~/.ssh/id_work -C "alice@company.com"
# Add to your GitHub account:# Go to https://github.com/settings/keys# New SSH key → paste the contents of ~/.ssh/id_work.pubVerify SSH key is active
Section titled “Verify SSH key is active”After switching:
gitswitch workgit config --global core.sshCommand# → ssh -i ~/.ssh/id_work -o IdentitiesOnly=yesTest the key:
ssh -T git@github.com# → Hi alice-work! You've successfully authenticated...Multiple keys per platform
Section titled “Multiple keys per platform”If you use multiple GitHub accounts:
# Personal accountgitswitch add personal "Alice" alice@gmail.com \ --ssh-key ~/.ssh/id_personal
# Work accountgitswitch add work "Alice" alice@company.com \ --ssh-key ~/.ssh/id_workEach profile uses its own key. When you switch, the key switches too.
SSH key passphrase
Section titled “SSH key passphrase”If your SSH key has a passphrase:
- Add it to your SSH agent once:
ssh-add ~/.ssh/id_work# Enter passphrase when prompted- gitswitch will use the agent’s unlocked key.
Troubleshooting
Section titled “Troubleshooting”Permission denied (publickey)
Section titled “Permission denied (publickey)”Permission denied (publickey).fatal: Could not read from remote repository.Check:
- Is the SSH key path correct?
- Is the key added to your GitHub account?
- Is the passphrase in your agent?
# Verify the key pathgitswitch list work # should show SSH key
# Test SSHssh -i ~/.ssh/id_work -T git@github.com
# Add to agent if neededssh-add ~/.ssh/id_workSlow authentication
Section titled “Slow authentication”If SSH is slow, it might be trying multiple keys. Verify IdentitiesOnly=yes is set:
git config --global core.sshCommand# Must include: -o IdentitiesOnly=yesSSH agent not working
Section titled “SSH agent not working”If you don’t want to use an agent:
# Generate key without passphrasessh-keygen -t ed25519 -f ~/.ssh/id_work -N "" -C "alice@company.com"Per-repo SSH key override
Section titled “Per-repo SSH key override”If you need a different key for a specific repo:
cd ~/special-repogit config --local core.sshCommand \ "ssh -i ~/.ssh/id_special -o IdentitiesOnly=yes"Local config takes priority over global.