Setting Up Two GitHub Accounts on macOS

Use SSH aliases and conditional Git configuration to separate personal and work GitHub accounts.

On this page

Setting Up Two GitHub Accounts on macOS

I got a MacBook Pro M4 at my new job. Setting up a new machine is already a project, and I also needed to juggle a second GitHub account. Here is how I set up SSH and Git so the machine can pick the right identity.

How the accounts fit together

  • My personal account is chungchihhan on github.com.
  • My work account is harry_chung on a self-hosted GitHub Enterprise server. The examples use github.mycompany.com as a placeholder.
💻 MacBook Air M3 (個人專用)
├── [SSH Key & Host 設定]
│   ├── Host 別名:personal
│   ├── HostName:github.com
│   └── IdentityFile:~/.ssh/id_m3_personal
└── [Git 身份設定]
    └── 全域設定 (Global):user.name=chungchihhan, user.email=個人信箱

💻 MacBook Pro M4 (個人 & 公司混用)
├── [SSH Keys & Host 設定]
│   ├── Host 別名:personal
│   │   ├── HostName:github.com
│   │   └── IdentityFile:~/.ssh/id_m4_personal (個人 Key)
│   └── Host 別名:work
│       ├── HostName:github.mycompany.com
│       └── IdentityFile:~/.ssh/id_m4_work (公司 Key)
│
└── [Git 身份設定:條件式包含 (includeIf)]
    ├── ❌ 全域設定 (Global):無 user.name/email (已設定)
    ├── [條件 1] 路徑:~/Developer/Personal/
    │   └── 載入:~/.gitconfig-personal (user.name=chungchihhan, user.email=個人信箱)
    └── [條件 2] 路徑:~/Developer/Work/
        └── 載入:~/.gitconfig-work (user.name=harry_chung, user.email=公司信箱)
Text

Set up the personal MacBook Air M3

Step 1. Generate and upload an SSH key

  • Generate the key.
    ssh-keygen -t ed25519 -C "chungchihhan@macbook_air_m3" -f ~/.ssh/id_m3_personal
    Bash
  • Copy the public key.
    pbcopy < ~/.ssh/id_m3_personal.pub
    Bash
  • Upload it to your personal GitHub account. Open GitHub account settings Open SSH and GPG keys Add the public SSH key

Step 2. Configure SSH

  • Create or edit ~/.ssh/config.
    Host personal
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_m3_personal
    Text

Step 3. Store the passphrase and test the connection

  • Add the private key to the SSH agent and store its passphrase in Keychain.
    ssh-add -K ~/.ssh/id_m3_personal
    Bash
  • Test the connection.
    ssh -T git@personal
    Bash
    • GitHub should reply with a message beginning Hi chungchihhan! You've successfully authenticated....
    • SSH may ask you to confirm the server's host key on the first connection.

Step 4. Set the Git remote

  • This laptop only hosts personal projects. If you created chungchihhan/test-git.git on GitHub and already ran git init in a local directory, configure the remote as follows.
    # 使用別名 personal
    git remote set-url origin git@personal:chungchihhan/test-git.git
    # 或
    git remote add origin git@personal:chungchihhan/test-git.git
    Bash
  • Check the remote.
    git remote -v
    Bash
  • When you run git push --set-upstream origin main, SSH can use the passphrase in Keychain to unlock the private key. Git records the commit identity from the applicable Git configuration.

Set up the work MacBook Pro M4

Step 1. Generate and upload both SSH keys

  • Personal SSH key

    • Generate the key.
      ssh-keygen -t ed25519 -C "chungchihhan@macbook_pro_m4" -f ~/.ssh/id_m4_personal
      Text
    • Copy the public key.
      pbcopy < ~/.ssh/id_m4_personal.pub
      Bash
    • Upload it to your personal GitHub account.

      NOTE

      The upload steps are the same as on the personal MacBook. See the screenshots above.

  • Work SSH key

    • Generate the key.
      ssh-keygen -t ed25519 -C "harry_chung@mycompany.com_macbook_pro_m4" -f ~/.ssh/id_m4_work
      Text
    • Copy the public key.
      pbcopy < ~/.ssh/id_m4_work.pub
      Bash
    • Upload it to your work GitHub account, shown as https://github.mycompany.com/harry_chung in this example.

      NOTE

      The SSH key screen works the same way as in the personal account.

Step 2. Configure SSH

  • Create or edit ~/.ssh/config with one host alias per account.
    # MacBook Pro M4 - 個人帳號設定
    Host personal
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_m4_personal
    
    # MacBook Pro M4 - 公司帳號設定
    Host work
      HostName github.mycompany.com
      User git
      IdentityFile ~/.ssh/id_m4_work
    Text

Step 3. Store each passphrase and test both connections

  • Personal account

    • Add the key and store the passphrase.
      ssh-add -K ~/.ssh/id_m4_personal
      Bash
    • Test the connection.
      ssh -T git@personal
      Bash
      • The greeting should identify chungchihhan.
      • SSH may ask you to confirm the server's host key on the first connection.
  • Work account

    • Add the key and store the passphrase.
      ssh-add -K ~/.ssh/id_m4_work
      Bash
    • Test the connection.
      ssh -T git@work
      Bash
      • The greeting should identify harry_chung.
      • SSH may ask you to confirm the server's host key on the first connection.

Step 4. Set the commit identity with includeIf

IMPORTANT

This step applies to the work MacBook in my setup because it uses both accounts. Conditional configuration lets Git select the right user.name and user.email based on the project directory.

  • Create separate identity files in your home directory.
    • Contents of ~/.gitconfig-personal
      [user]
        name = chungchihhan
        email = 個人信箱
      Text
    • Contents of ~/.gitconfig-work
      [user]
        name = harry_chung
        email = harry_chung@mycompany.com
      Text
  • Configure the main ~/.gitconfig.
    • I keep personal and work projects in separate folders under ~/Developer.
      └── Developer
          ├── Personal
          └── Work
      Text
    • My .gitconfig includes each identity file conditionally.
      [includeIf "gitdir:~/Developer/Personal/"]
          path = ~/.gitconfig-personal
      [includeIf "gitdir:~/Developer/Work/"]
          path = ~/.gitconfig-work
      Text
  • Check the identity. In a project directory, run git init, then git config --list --show-origin. Confirm that the expected user.name and user.email come from the correct file.

Step 5. Set the Git remote

  • For a personal repo such as chungchihhan/test-git.git, configure the remote from a local repository you have already initialized.
    # 使用別名 personal
    git remote set-url origin git@personal:chungchihhan/test-git.git
    # 或
    git remote add origin git@personal:chungchihhan/test-git.git
    Bash
  • For a work repo such as harry-chung/test-git.git, configure the remote from its own initialized directory.
    # 使用別名 work
    git remote add origin git@work:harry-chung/test-git.git
    Bash
  • The same command checks the remote in either repository.
    git remote -v
    Bash
  • Run git push --set-upstream origin main when ready. The remote's SSH alias selects the authentication key, and the project directory selects the commit identity.

Summary

The personal and work host aliases tell SSH which private key to use. Keychain can supply the saved passphrases. Git's includeIf rules handle the separate question of which name and email go into each commit.

  • Under ~/Developer/Personal/, Git uses my chungchihhan identity.
  • Under ~/Developer/Work/, Git uses my harry_chung identity.

WARNING

Replace the example account names, email addresses, and company hostname with your own.