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
chungchihhanon github.com. - My work account is
harry_chungon a self-hosted GitHub Enterprise server. The examples usegithub.mycompany.comas 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=公司信箱)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_personalBash - Copy the public key.
pbcopy < ~/.ssh/id_m3_personal.pubBash - Upload it to your personal GitHub account.

Step 2. Configure SSH
- Create or edit
~/.ssh/config.Host personal HostName github.com User git IdentityFile ~/.ssh/id_m3_personalText
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_personalBash - Test the connection.
ssh -T git@personalBash- 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.
- GitHub should reply with a message beginning
Step 4. Set the Git remote
- This laptop only hosts personal projects. If you created
chungchihhan/test-git.giton GitHub and already rangit initin 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.gitBash - Check the remote.
git remote -vBash - 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_personalText - Copy the public key.
pbcopy < ~/.ssh/id_m4_personal.pubBash - Upload it to your personal GitHub account.
NOTE
The upload steps are the same as on the personal MacBook. See the screenshots above.
- Generate the key.
-
Work SSH key
- Generate the key.
ssh-keygen -t ed25519 -C "harry_chung@mycompany.com_macbook_pro_m4" -f ~/.ssh/id_m4_workText - Copy the public key.
pbcopy < ~/.ssh/id_m4_work.pubBash - Upload it to your work GitHub account, shown as
https://github.mycompany.com/harry_chungin this example.NOTE
The SSH key screen works the same way as in the personal account.
- Generate the key.
Step 2. Configure SSH
- Create or edit
~/.ssh/configwith 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_workText
Step 3. Store each passphrase and test both connections
-
Personal account
- Add the key and store the passphrase.
ssh-add -K ~/.ssh/id_m4_personalBash - Test the connection.
ssh -T git@personalBash- The greeting should identify
chungchihhan. - SSH may ask you to confirm the server's host key on the first connection.
- The greeting should identify
- Add the key and store the passphrase.
-
Work account
- Add the key and store the passphrase.
ssh-add -K ~/.ssh/id_m4_workBash - Test the connection.
ssh -T git@workBash- The greeting should identify
harry_chung. - SSH may ask you to confirm the server's host key on the first connection.
- The greeting should identify
- Add the key and store the passphrase.
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.comText
- Contents of
- Configure the main
~/.gitconfig.- I keep personal and work projects in separate folders under
~/Developer.└── Developer ├── Personal └── WorkText - My
.gitconfigincludes each identity file conditionally.[includeIf "gitdir:~/Developer/Personal/"] path = ~/.gitconfig-personal [includeIf "gitdir:~/Developer/Work/"] path = ~/.gitconfig-workText
- I keep personal and work projects in separate folders under
- Check the identity. In a project directory, run
git init, thengit config --list --show-origin. Confirm that the expecteduser.nameanduser.emailcome 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.gitBash - 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.gitBash - The same command checks the remote in either repository.
git remote -vBash - Run
git push --set-upstream origin mainwhen 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 mychungchihhanidentity. - Under
~/Developer/Work/, Git uses myharry_chungidentity.
WARNING
Replace the example account names, email addresses, and company hostname with your own.