Date: 2026-01-08 00:00 Summary: Documented macOS multi-GitHub-account setup for fetch/push via separate SSH Hosts.
[question] 有多个github账户,在mac上怎么配置fetch和push不同的账户
[try to solve] On macOS, the most reliable approach for multiple GitHub accounts is separate SSH keys and Host aliases.
Per-account SSH key + Host alias
- Generate keys (example: work and personal):
ssh-keygen -t ed25519 -C "work@example.com" -f ~/.ssh/id_ed25519_work
ssh-keygen -t ed25519 -C "personal@example.com" -f ~/.ssh/id_ed25519_personal
Add each public key to the matching GitHub account (Settings → SSH and GPG keys).
Configure
~/.ssh/config:
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
AddKeysToAgent yes
UseKeychain yes
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
AddKeysToAgent yes
UseKeychain yes
- Point each repo’s remote at the matching Host:
# Work repo
git remote set-url origin git@github-work:WorkOrg/repo.git
# Personal repo
git remote set-url origin git@github-personal:PersonalUser/repo.git
Fetch/push then pick the correct key via the Host alias.
Optional: per-repo author email
git config user.name "Your Name"
git config user.email "work@example.com" # in the work repo
# Set personal email in the personal repo
HTTPS (not preferred for multi-account)
Possible via Keychain / host aliases / credential helpers, but SSH is simpler and recommended here.