github multiple users management

1503 words, 5 mins

Managing Multiple GitHub Accounts Locally

Problem

When you need to use multiple GitHub accounts on the same computer, you’ll face challenges with SSH keys and authentication.

Solution: SSH Keys Method

1. Generate Different SSH Keys

# For your personal account
ssh-keygen -t ed25519 -C "personal@example.com" -f ~/.ssh/id_personal

# For your work account
ssh-keygen -t ed25519 -C "work@example.com" -f ~/.ssh/id_work

2. Add Keys to SSH Agent

ssh-add ~/.ssh/id_personal
ssh-add ~/.ssh/id_work

3. Configure SSH

Create or edit ~/.ssh/config:

# Personal GitHub account
Host github.com-personal
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_personal
   IdentitiesOnly yes

# Work GitHub account
Host github.com-work
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_work
   IdentitiesOnly yes

4. Add SSH Keys to GitHub Accounts

Copy the public keys and add them to the respective GitHub accounts:

cat ~/.ssh/id_personal.pub
cat ~/.ssh/id_work.pub

5. Configure Git for Each Repository

# For personal projects
git config user.name "Personal Name"
git config user.email "personal@example.com"

# For work projects
git config user.name "Work Name"
git config user.email "work@example.com"

6. Clone Repositories with Correct Host

# For personal repositories
git clone git@github.com-personal:username/repo.git

# For work repositories
git clone git@github.com-work:company/repo.git

Alternative: HTTPS with Credential Manager

If you prefer HTTPS over SSH:

  1. Configure credential manager to store credentials
  2. Use different credentials for different repositories
  3. Use Git’s credential context for different directories
git config --global credential.useHttpPath true

Testing Your Setup

ssh -T git@github.com-personal
ssh -T git@github.com-work