Skip to content

Instantly share code, notes, and snippets.

@shawonis08
Created December 2, 2025 20:41
Show Gist options
  • Select an option

  • Save shawonis08/69d062a53d2ae54a0565e9c8d6aadcc1 to your computer and use it in GitHub Desktop.

Select an option

Save shawonis08/69d062a53d2ae54a0565e9c8d6aadcc1 to your computer and use it in GitHub Desktop.
Complete guide to managing multiple GitHub accounts on Same Machine using SSH keys with automatic Git user configuration. Set it once, forget about it - personal and work commits use the correct email automatically based on directory location.

Managing Multiple GitHub Accounts (SSH + Auto Config)

A complete guide to set up and manage personal and work GitHub accounts on the same machine using SSH keys with automatic Git configuration.

Prerequisites

  • Linux/Mac
  • Git installed
  • Access to both GitHub accounts
  • Terminal/command line knowledge

Step 1: Generate SSH Keys for Both Accounts

Personal Account SSH Key

# Generate SSH key for personal account
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_personal

# Start SSH agent
eval "$(ssh-agent -s)"

# Add key to SSH agent
ssh-add ~/.ssh/id_ed25519_personal

Work Account SSH Key

# Generate SSH key for work account
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_work

# Add key to SSH agent
ssh-add ~/.ssh/id_ed25519_work

Note: When prompted for a passphrase, you can either:

  • Add one for extra security (you'll need to enter it when using the key)
  • Press Enter to skip (easier but less secure)

Step 2: Add SSH Keys to GitHub Accounts

For Personal Account

  1. Copy the public key:

    cat ~/.ssh/id_ed25519_personal.pub | pbcopy
  2. Go to GitHub (personal account):

    • Settings → SSH and GPG keys → New SSH key
    • Title: "Personal Mac" (or any identifier)
    • Paste the key and save

For Work Account

  1. Copy the public key:

    cat ~/.ssh/id_ed25519_work.pub | pbcopy
  2. Go to GitHub (work account):

    • Settings → SSH and GPG keys → New SSH key
    • Title: "Work Mac" (or any identifier)
    • Paste the key and save

Step 3: Configure SSH to Use Different Keys

Create or edit SSH config file:

nano ~/.ssh/config

Add the following configuration:

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

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

Important: The IdentitiesOnly yes ensures SSH only uses the specified key for each host.

Save and exit: Ctrl+X, then Y, then Enter


Step 4: Test SSH Connections

# Test personal account
ssh -T [email protected]
# Should show: Hi your-personal-username! You've successfully authenticated...

# Test work account
ssh -T git@github-work
# Should show: Hi your-work-username! You've successfully authenticated...

If both show different usernames, you're all set! ✅


Step 5: Set Up Automatic Git Configuration

Set Personal Info as Global Default

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

Create Work Directory Structure

# Create a dedicated directory for all work projects
mkdir -p ~/Work

Configure Automatic Work Settings

Edit global Git config:

nano ~/.gitconfig

Add this at the end of the file:

[includeIf "gitdir:~/Work/"]
    path = ~/.gitconfig-work

Create work-specific config:

nano ~/.gitconfig-work

Add your work details:

[user]
    name = Your Name
    email = [email protected]

Step 6: Cloning Repositories

Personal Repositories

Clone normally from anywhere:

git clone [email protected]:your-username/repo-name.git

Work Repositories

Clone into the ~/Work directory using the github-work host:

cd ~/Work
git clone git@github-work:company/repo-name.git

Key differences:

  • Personal: [email protected]:username/repo.git
  • Work: git@github-work:company/repo.git (note the github-work host)

Verification

Check SSH Keys

# List all SSH keys in agent
ssh-add -l

Check Git Configuration

# Check global config
git config --global --list

# Check config in a personal repo
cd ~/personal-repo
git config user.email  # Should show personal email

# Check config in a work repo
cd ~/Work/work-repo
git config user.email  # Should show work email

Common Issues and Solutions

Issue 1: Wrong account authenticating

Problem: Both ssh -T commands show the same username

Solution: Make sure IdentitiesOnly yes is in your ~/.ssh/config and restart terminal

Issue 2: Permission denied (publickey)

Problem: Can't authenticate with GitHub

Solutions:

# Check if keys are added to SSH agent
ssh-add -l

# If not, add them again
ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_work

# Verify the key is added to GitHub account

Issue 3: Wrong email in commits

Problem: Commits show wrong email address

Solution:

# Check current repo config
git config user.email

# If wrong, set it manually for this repo
git config user.email "[email protected]"

# Or move the repo to correct directory (~/Work for work repos)

Issue 4: Can't push to work repository

Problem: Permission denied or Repository not found

Solution: Make sure you're using the correct host

# Check remote URL
git remote -v

# If showing github.com instead of github-work, update it:
git remote set-url origin git@github-work:company/repo-name.git

Quick Reference Commands

# Test SSH connections
ssh -T [email protected]          # Personal
ssh -T git@github-work         # Work

# Clone repositories
git clone [email protected]:user/repo.git              # Personal
git clone git@github-work:company/repo.git          # Work (in ~/Work)

# Check Git config
git config user.email          # Check current repo
git config --global user.email # Check global

# View SSH keys
ssh-add -l                     # List loaded keys
ls -la ~/.ssh/                 # List all SSH files

# Update remote URL (if needed)
git remote set-url origin git@github-work:company/repo.git

Directory Structure Example

~/
├── Work/                          # All work projects (auto-uses work email)
│   ├── project-1/
│   ├── project-2/
│   └── project-3/
├── personal-project-1/            # Personal projects (uses personal email)
├── personal-project-2/
└── .ssh/
    ├── config                     # SSH configuration
    ├── id_ed25519_personal        # Personal private key
    ├── id_ed25519_personal.pub    # Personal public key
    ├── id_ed25519_work            # Work private key
    └── id_ed25519_work.pub        # Work public key

Best Practices

  1. Always clone work repos into ~/Work directory - automatic email configuration
  2. Use descriptive SSH key comments - helps identify keys later
  3. Test SSH connection before cloning - prevents authentication issues
  4. Keep SSH keys secure - never share private keys
  5. Verify commit author before pushing - run git config user.email to check
  6. Use different key names - easier to manage and identify

Security Tips

  • ✅ Add passphrases to SSH keys for extra security
  • ✅ Never commit private keys (files without .pub extension)
  • ✅ Regularly review SSH keys in GitHub settings
  • ✅ Remove old/unused SSH keys from GitHub
  • ✅ Use different SSH keys for different accounts (never reuse)

Summary

With this setup:

  • ✅ Personal repos automatically use personal email
  • ✅ Work repos (in ~/Work) automatically use work email
  • ✅ Each account uses its own SSH key
  • ✅ No manual configuration needed per repository
  • ✅ Clear separation between work and personal projects

Troubleshooting Checklist

If something doesn't work:

  • SSH keys generated for both accounts?
  • Public keys added to respective GitHub accounts?
  • ~/.ssh/config file created with correct configuration?
  • IdentitiesOnly yes present in SSH config?
  • SSH agent running and keys added?
  • Tested SSH connection for both accounts?
  • ~/.gitconfig-work file created?
  • Work repos cloned into ~/Work directory?
  • Using correct host (github-work) for work repos?

Contributing

Feel free to suggest improvements or report issues!

License

MIT License - feel free to use and modify as needed.


Last Updated: 3rd December 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment