# Managing Multiple GitHub/Git Accounts on One Machine (Personal + Work)

As developers and DevOps engineers, it's common to contribute to both personal and professional projects. However, using **two GitHub accounts** on a **single machine** can lead to `identity conflicts`, `unverified commits`, or `accidentally pushing to the wrong repository`.

In this guide I’ll walks you through setting up two GitHub accounts **securely**, **cleanly**, and with **verified SSH-signed commits** all on a single machine.

## Objectives

1. Work with both **personal and work GitHub repositories** (public & private).
    
2. Use **two separate GitHub accounts** on one machine.
    
3. Ensure **verified commits** via SSH commit signing.
    
4. Maintain a **scalable, secure, and professional Git setup**.
    

## Step 1: Generate Separate SSH Keys

Create separate SSH key pairs for each GitHub account:

You can name these according to your preferences while generating the SSH keys

* `for-personal`
    
* `for-work`
    

```bash
# Personal Key
ssh-keygen -t ed25519 -C "you.email@gmail.com" -f ~/.ssh/for-personal

# Work Key
ssh-keygen -t ed25519 -C "you.workemail@gmail.com" -f ~/.ssh/for-work
```

> Do **not** overwrite the default `id_ed25519`. Keeping keys separate ensures flexibility and security.

## Step 2: Add Keys to SSH Agent

```bash
# View loaded keys
ssh-add -l

# Add new keys
ssh-add ~/.ssh/for-personal
ssh-add ~/.ssh/for-work

# Remove all existing keys (optional reset)
ssh-add -D
```

## Step 3: Configure SSH `config` File

Create or edit your SSH config file:

```bash
touch ~/.ssh/config
```

Add the following content:

```ini
# Global settings
Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentitiesOnly yes
  ServerAliveInterval 60
  ServerAliveCountMax 3

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

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

## Step 4: Add Public Keys to GitHub

Upload the corresponding `.pub` files to each account:

* `~/.ssh/for-personal.pub` → Personal GitHub
    
* `~/.ssh/for-work.pub` → Work GitHub
    

> GitHub → **Settings → SSH and GPG Keys → New SSH Key**

## Step 5: Clone Repositories Using SSH Host Aliases

```bash
# Clone personal repo
git clone git@personal.github.com:your-username/my-repo.git

# Clone work repo
git clone git@work.github.com:my-org/work-repo.git
```

## Step 6: Configure Git Identity per Repository

Set identity locally inside each project to avoid global conflicts:

```bash
# Inside Personal Repo
cd ~/PersonalRepo

git config user.name "your name"
git config user.email "your email"

# Inside Work Repo
cd ~/WorkRepo

git config user.name "your name"
git config user.email "your work email"
```

## Step 7: Enable Verified Commits with SSH Signing

GitHub supports **SSH-based commit signing**, separate from SSH authentication.

### Generate Signing Keys

```bash
# Personal Signing Key
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_signing_personal -C "signing-personal"

# Work Signing Key
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_signing_work -C "signing-work"
```

### Add Signing Keys to GitHub

Go to **GitHub → Settings → SSH and GPG Keys → New Signing Key** and paste the contents of each `.pub` file.

## Step 8: Configure Git to Use Signing Keys

Set up commit signing in each repo:

```bash
# Personal Repo
cd ~/PersonalRepo

git config commit.gpgsign true
git config gpg.format ssh
git config user.signingkey ~/.ssh/id_ed25519_signing_personal.pub

# Work Repo
cd ~/WorkRepo

git config commit.gpgsign true
git config gpg.format ssh
git config user.signingkey ~/.ssh/id_ed25519_signing_work.pub
```

## Pre-Commit Workflow: Start SSH Agent and Test Connections

Before making commits in any repository, ensure your SSH agent is running and keys are loaded:

```bash
# Ensure SSH agent is running and keys are loaded
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/personal_github
ssh-add ~/.ssh/work_github

# Test connections to verify which account will be used
ssh -T git@personal.github.com
# or
ssh -T git@work.github.com

# Now proceed with your git operations
git add .
git commit -m "Your commit message"
git push origin main
```

> Successful messages like `Hi broh! You've successfully authenticated` confirm proper setup.

---

**Note:** This step is particularly important after system restarts or when opening new terminal sessions, as the SSH agent may not be running or may not have your keys loaded.

## Final Result

* **Secure and clean GitHub SSH setup** for both accounts.
    
* **Verified commits** using SSH signing keys.
    
* **Separate identities** per project.
    

### Bonus Tip

To view signed commits:

```bash
git log --show-signature
```

## The end! - Thanks for reading…
