Brain
EngineeringGit

Use more than one GitHub or GitLab account on one machine

Give each account its own SSH key and a host alias so git picks the right identity per repo.

Two accounts on one machine, personal and work, both pushing to github.com. SSH can't tell which key belongs to which. The fix: one key per account, plus a host alias so the clone URL says which identity to use.

1. Make a key per account

Name it after the account:

ssh-keygen -t ed25519 -C "email@email.com" -f ~/.ssh/id_work

Add the public key (id_work.pub) to that account under Settings โ†’ SSH keys.

2. Add a host alias in ~/.ssh/config

Give each account a made-up host suffix. Both point at the real HostName:

Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_work

Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_personal

Keep the suffix consistent. Whatever you write here (github.com-work) must appear in every URL for that account, or git falls back to the wrong key.

3. Use the alias in the URL

Clone with the alias in place of github.com:

git clone git@github.com-work:username/reponame.git

For a repo you already have:

git remote set-url origin git@github.com-work:username/reponame.git

4. Set the right name and email per repo

Global config only covers one account. Override it inside each repo:

git config user.name "Your Name"
git config user.email "work@email.com"

Test it

ssh -T git@github.com-work

It should greet you as the right user. If it uses the wrong key, run ssh-add ~/.ssh/id_work and try again.

GitLab works the same way: swap github.com for gitlab.com in the config and the URLs.

On this page