Git and SSH

Use two GitHub accounts on one machine

Keep a work and a personal GitHub account side by side, each with its own SSH key.

Fill these in once
  1. Create a second key just for this account
    Terminal
    ssh-keygen -t ed25519 -C "you@work.com" -f ~/.ssh/id_ed25519_work
  2. Give it its own short name in your SSH settings
    Terminal
    cat >> ~/.ssh/config <<'EOF'
    Host github-work
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_ed25519_work
      IdentitiesOnly yes
    EOF
  3. Copy the public key and add it to the second account
    Terminal
    pbcopy < ~/.ssh/id_ed25519_work.pub
    open https://github.com/settings/ssh/new

    Make sure you're logged in to the second account in the browser.

  4. Clone using the short name instead of github.com
    Terminal
    git clone git@github-work:company/project.git
  5. Use the work email for commits in that repo
    Terminal
    git -C "$(basename company/project)" config user.email "you@work.com"

Check it worked

  1. Check GitHub sees the second account
    Terminal
    ssh -T git@github-work

    It should greet you with the second account's username.

  1. Create a second key just for this account
    Terminal · Ubuntu / Debian
    ssh-keygen -t ed25519 -C "you@work.com" -f ~/.ssh/id_ed25519_work
  2. Give it its own short name in your SSH settings
    Terminal · Ubuntu / Debian
    cat >> ~/.ssh/config <<'EOF'
    Host github-work
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_ed25519_work
      IdentitiesOnly yes
    EOF
  3. Show the public key, then add it to the second account
    Terminal · Ubuntu / Debian
    cat ~/.ssh/id_ed25519_work.pub
    xdg-open https://github.com/settings/ssh/new

    Make sure you're logged in to the second account in the browser.

  4. Clone using the short name instead of github.com
    Terminal · Ubuntu / Debian
    git clone git@github-work:company/project.git
  5. Use the work email for commits in that repo
    Terminal · Ubuntu / Debian
    git -C "$(basename company/project)" config user.email "you@work.com"

Check it worked

  1. Check GitHub sees the second account
    Terminal · Ubuntu / Debian
    ssh -T git@github-work

    It should greet you with the second account's username.

  1. Create a second key just for this account
    PowerShell
    ssh-keygen -t ed25519 -C "you@work.com" -f "$env:USERPROFILE\.ssh\id_ed25519_work"
  2. Give it its own short name in your SSH settings
    PowerShell
    Add-Content "$env:USERPROFILE\.ssh\config" @"
    Host github-work
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_ed25519_work
      IdentitiesOnly yes
    "@
  3. Copy the public key and add it to the second account
    PowerShell
    Get-Content "$env:USERPROFILE\.ssh\id_ed25519_work.pub" | Set-Clipboard
    Start-Process "https://github.com/settings/ssh/new"

    Make sure you're logged in to the second account in the browser.

  4. Clone using the short name instead of github.com
    PowerShell
    git clone git@github-work:company/project.git
  5. Use the work email for commits in that repo
    PowerShell
    git -C (Split-Path "company/project" -Leaf) config user.email "you@work.com"

Check it worked

  1. Check GitHub sees the second account
    PowerShell
    ssh -T git@github-work

    It should greet you with the second account's username.