Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sachithrrra/0e7d22f72767bb4fcb48f26235d52b06 to your computer and use it in GitHub Desktop.
Save sachithrrra/0e7d22f72767bb4fcb48f26235d52b06 to your computer and use it in GitHub Desktop.
Managing Multiple GitHub Accounts on macOS

Managing Multiple GitHub Accounts on macOS

1. Create SSH keys

cd ~/.ssh
ssh-keygen -t rsa -C "my@personal" -f "github-personal"
ssh-keygen -t rsa -C "my@work" -f "github-work"

2. Add keys to SSH agent

ssh-add --apple-use-keychain ~/.ssh/github-personal
ssh-add --apple-use-keychain ~/.ssh/github-work

Copy public keys to clipboard:

pbcopy < ~/.ssh/github-personal.pub  # Add to personal GitHub account
pbcopy < ~/.ssh/github-work.pub      # Add to work GitHub account

3. Configure SSH

Create or edit ~/.ssh/config:

open -e ~/.ssh/config

Add these host configurations:

# Personal GitHub Account
Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/github-personal

# Work GitHub Account
Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/github-work

Set default account globally:

git config --global user.name "your_personal_username"
git config --global user.email "[email protected]"

4. Use multiple accounts

Clone new repositories:

git clone [email protected]:owner/repo.git  # For personal
git clone [email protected]:owner/repo.git      # For work

Update existing repositories:

git remote rm origin
git remote add origin [email protected]:owner/repo.git
git remote set-url origin [email protected]:owner/repo.git

Set repository-specific user:

Default git info set to personal, so for work projects manually change git user.name and user.email

git config user.email "[email protected]"
git config user.name "Your Name"

5. Check keys after restart

ssh-add -l  # Check if keys are loaded

If keys missing, add them again:

ssh-add --apple-use-keychain ~/.ssh/github-personal
ssh-add --apple-use-keychain ~/.ssh/github-work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment