Skip to content
$ gitswitch
Nord
★ star

Multi-account GitHub

This guide covers maintaining multiple GitHub accounts on a single machine.

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

One key per account:

Terminal window
# Personal account
ssh-keygen -t ed25519 -f ~/.ssh/id_personal -C "alice@gmail.com"
# Work account
ssh-keygen -t ed25519 -f ~/.ssh/id_work -C "alice@company.com"

Personal account (alice):

  1. Go to https://github.com/settings/keys
  2. Add New SSH key
  3. Paste contents of ~/.ssh/id_personal.pub
  4. Save

Work account (alice-corp):

  1. Log into your work account
  2. Go to https://github.com/settings/keys
  3. Add New SSH key
  4. Paste contents of ~/.ssh/id_work.pub
  5. Save
Terminal window
# Personal account
ssh -i ~/.ssh/id_personal -T git@github.com
# → Hi alice! You've successfully authenticated...
# Work account
ssh -i ~/.ssh/id_work -T git@github.com
# → Hi alice-corp! You've successfully authenticated...
Terminal window
# Personal profile
gitswitch add personal "Alice" alice@gmail.com \
--ssh-key ~/.ssh/id_personal \
--gh-user alice
# Work profile
gitswitch add work "Alice" alice@company.com \
--ssh-key ~/.ssh/id_work \
--gh-user alice-corp
Terminal window
# Clone personal project
gitswitch personal
git clone git@github.com:alice/my-project.git
cd my-project
# Clone work project
gitswitch work
git clone git@github.com:alice-corp/company-repo.git
cd company-repo
Terminal window
cd ~/projects/my-library
gitswitch personal
# Verify identity
gitswitch current
# → personal / alice@gmail.com
# Commit - attributed to alice@gmail.com using id_personal
git commit -m "Add feature"
git push
Terminal window
cd ~/work/internal-api
gitswitch work
# Verify identity
gitswitch current
# → work / alice@company.com
# Commit - attributed to alice@company.com using id_work
git commit -m "Fix bug"
git push

Pin each repo to its account:

Terminal window
cd ~/projects/my-library
gitswitch pin personal
cd ~/work/internal-api
gitswitch pin work

Now when you cd into these directories, gitswitch suggests the right account:

Terminal window
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]

With both accounts authenticated, you can check notifications:

Terminal window
# Personal account notifications
gitswitch personal
gh notification list
# Work account notifications
gitswitch work
gh notification list

If you clone the same GitHub repo in different locations:

Terminal window
# Clone 1: Working copy (as personal)
cd ~/projects/my-library
gitswitch personal
git clone git@github.com:alice/my-library.git main
cd main
gitswitch pin personal
# Clone 2: Backup/archive (still as personal)
cd ~/backup
gitswitch personal
git clone git@github.com:alice/my-library.git archive
cd archive
gitswitch pin personal
# Each maintains its own identity preference

Scenario: 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:

Terminal window
# The repo is under personal account
git clone git@github.com:alice/open-project.git
cd open-project
# But alice-corp wants to contribute
gitswitch work
# Now commits are attributed to alice@company.com (work account)
git commit -m "Add contribution"
git push

The 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:

Terminal window
cd open-project
gitswitch personal
# Now commits are from alice@gmail.com (personal account)
git commit -m "Add contribution"
git push

If you pushed with the wrong account:

Terminal window
# Check current identity
gitswitch current
# If wrong, switch and amend
gitswitch personal
git commit --amend --reset-author
git push --force-with-lease # If not yet pulled by others
Permission denied (publickey)
fatal: Could not read from remote repository.

Check:

  1. Is the correct identity active?
  2. Is the SSH key added to that GitHub account?
Terminal window
gitswitch current
# Should show the account you're pushing to
# Test SSH
ssh -i ~/.ssh/id_personal -T git@github.com
# Should show: Hi alice! You've successfully authenticated...
# Check git is using the right key
git config --global core.sshCommand

Check the GitHub remote:

Terminal window
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 commits
git log -3 --format="%an <%ae>"
# Shows which accounts were used recently

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:

Terminal window
cd ~/github/personal/*
# gitswitch nudges to personal
cd ~/github/work/*
# gitswitch nudges to work

Advanced: Automatic switching by directory pattern

Section titled “Advanced: Automatic switching by directory pattern”

Add to ~/.zshrc:

Terminal window
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.

Open Source + Work
Multi-client Freelancer
Quick Start