Multi-account GitHub
This guide covers maintaining multiple GitHub accounts on a single machine.
The scenario
Section titled “The scenario”You have:
- A personal GitHub account (
alice) - A work GitHub account (
alice-corp) - Both accounts might have repos you actively maintain
You want to:
- Clone repos from either account
- Push commits from the correct account
- Automatically use the right SSH key and identity
1. Generate unique SSH keys
Section titled “1. Generate unique SSH keys”One key per account:
# Personal accountssh-keygen -t ed25519 -f ~/.ssh/id_personal -C "alice@gmail.com"
# Work accountssh-keygen -t ed25519 -f ~/.ssh/id_work -C "alice@company.com"2. Add keys to GitHub
Section titled “2. Add keys to GitHub”Personal account (alice):
- Go to https://github.com/settings/keys
- Add New SSH key
- Paste contents of
~/.ssh/id_personal.pub - Save
Work account (alice-corp):
- Log into your work account
- Go to https://github.com/settings/keys
- Add New SSH key
- Paste contents of
~/.ssh/id_work.pub - Save
3. Test SSH authentication
Section titled “3. Test SSH authentication”# Personal accountssh -i ~/.ssh/id_personal -T git@github.com# → Hi alice! You've successfully authenticated...
# Work accountssh -i ~/.ssh/id_work -T git@github.com# → Hi alice-corp! You've successfully authenticated...4. Create gitswitch profiles
Section titled “4. Create gitswitch profiles”# Personal profilegitswitch add personal "Alice" alice@gmail.com \ --ssh-key ~/.ssh/id_personal \ --gh-user alice
# Work profilegitswitch add work "Alice" alice@company.com \ --ssh-key ~/.ssh/id_work \ --gh-user alice-corpDaily workflow
Section titled “Daily workflow”Cloning from different accounts
Section titled “Cloning from different accounts”# Clone personal projectgitswitch personalgit clone git@github.com:alice/my-project.gitcd my-project
# Clone work projectgitswitch workgit clone git@github.com:alice-corp/company-repo.gitcd company-repoWorking on personal projects
Section titled “Working on personal projects”cd ~/projects/my-librarygitswitch personal
# Verify identitygitswitch current# → personal / alice@gmail.com
# Commit - attributed to alice@gmail.com using id_personalgit commit -m "Add feature"git pushWorking on work projects
Section titled “Working on work projects”cd ~/work/internal-apigitswitch work
# Verify identitygitswitch current# → work / alice@company.com
# Commit - attributed to alice@company.com using id_workgit commit -m "Fix bug"git pushSetting up automatic switching per repo
Section titled “Setting up automatic switching per repo”Pin each repo to its account:
cd ~/projects/my-librarygitswitch pin personal
cd ~/work/internal-apigitswitch pin workNow when you cd into these directories, gitswitch suggests the right account:
cd ~/projects/my-library# gitswitch: this repo usually uses personal — switch? [y/N]
cd ~/work/internal-api# gitswitch: this repo usually uses work — switch? [y/N]Managing GitHub notifications
Section titled “Managing GitHub notifications”With both accounts authenticated, you can check notifications:
# Personal account notificationsgitswitch personalgh notification list
# Work account notificationsgitswitch workgh notification listHandling multiple clones of same repo
Section titled “Handling multiple clones of same repo”If you clone the same GitHub repo in different locations:
# Clone 1: Working copy (as personal)cd ~/projects/my-librarygitswitch personalgit clone git@github.com:alice/my-library.git maincd maingitswitch pin personal
# Clone 2: Backup/archive (still as personal)cd ~/backupgitswitch personalgit clone git@github.com:alice/my-library.git archivecd archivegitswitch pin personal
# Each maintains its own identity preferenceScenario: Contributing to repos owned by others
Section titled “Scenario: Contributing to repos owned by others”If someone with the alice-corp account contributes to the alice account’s repo:
# The repo is under personal accountgit clone git@github.com:alice/open-project.gitcd open-project
# But alice-corp wants to contributegitswitch work# Now commits are attributed to alice@company.com (work account)
git commit -m "Add contribution"git pushThe contribution will show as coming from alice-corp account (because SSH auth and git config are switched).
If you want the contribution under the personal account:
cd open-projectgitswitch personal# Now commits are from alice@gmail.com (personal account)
git commit -m "Add contribution"git pushTroubleshooting
Section titled “Troubleshooting”Wrong account pushing
Section titled “Wrong account pushing”If you pushed with the wrong account:
# Check current identitygitswitch current
# If wrong, switch and amendgitswitch personalgit commit --amend --reset-authorgit push --force-with-lease # If not yet pulled by othersSSH key permission denied
Section titled “SSH key permission denied”Permission denied (publickey)fatal: Could not read from remote repository.Check:
- Is the correct identity active?
- Is the SSH key added to that GitHub account?
gitswitch current# Should show the account you're pushing to
# Test SSHssh -i ~/.ssh/id_personal -T git@github.com# Should show: Hi alice! You've successfully authenticated...
# Check git is using the right keygit config --global core.sshCommandConfused which account owns a repo
Section titled “Confused which account owns a repo”Check the GitHub remote:
git remote -v# Shows the URL, which indicates the account:# git@github.com:alice/... → personal account# git@github.com:alice-corp/... → work account
# Or check recent commitsgit log -3 --format="%an <%ae>"# Shows which accounts were used recentlyToo many repos to manage
Section titled “Too many repos to manage”With many repos across accounts, organization helps:
~/github/├── personal/ # All personal repos│ ├── project-a/│ ├── project-b/│ └── ...└── work/ # All work repos ├── internal-api/ ├── service-x/ └── ...Then:
cd ~/github/personal/*# gitswitch nudges to personal
cd ~/github/work/*# gitswitch nudges to workAdvanced: Automatic switching by directory pattern
Section titled “Advanced: Automatic switching by directory pattern”Add to ~/.zshrc:
function cd() { builtin cd "$@"
if [[ $PWD == *"/github/personal"* ]]; then gitswitch personal 2>/dev/null elif [[ $PWD == *"/github/work"* ]]; then gitswitch work 2>/dev/null fi}Now changing directories automatically switches accounts.
Next steps
Section titled “Next steps”→ Open Source + Work
→ Multi-client Freelancer
→ Quick Start