Developer workflow
Two GitHub accounts on one machine, behind a VPN
2 min read
Two GitHub accounts on one machine, behind a VPN
I use two GitHub accounts on the same laptop: a personal one for my own projects and a work one for company repositories. Most of the time that works fine with SSH keys. But when I work from China, where I connect through a VPN, the usual clone command started failing for me, and the fix turned out to be small enough to keep as a reference.
The symptom
With two accounts configured locally, cloning with the standard GitHub address did not work in that setup:
# Did not work for me behind the VPN with two accounts configured
git clone git@github.com:personal-user/repo.git
The fix: use the SSH host alias, not github.com
When you have more than one account, each one normally gets its own SSH key, and ~/.ssh/config defines a host alias per account that points to GitHub with the right key. Something along these lines (names are examples):
Host personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
Host work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
The key point is to use that alias in the remote URL instead of github.com. That way SSH applies the configuration of the alias, including which key to present:
# Personal account
git clone git@personal:personal-user/repo.git
# Work account
git clone git@work:work-org/repo.git
Using the alias consistently is what made cloning work again for both accounts in my case. It also has a nice side effect: it is always explicit which identity a repository uses.
Fallback: when Git cannot find the host
Sometimes Git simply could not resolve the host. When that happens, I switch that repository's remote to HTTPS with the username in the URL:
git remote set-url origin https://personal-user@github.com/personal-user/repo.git
Git then asks for a credential for that specific user over HTTPS. It is less elegant than SSH, but it gets a push through when the SSH route is not cooperating.
What I keep in mind
- With several accounts, never clone with plain
git@github.com:; always use the alias of the account that should own the repository. - If an existing repository was cloned with the wrong address, fix it with
git remote set-urlrather than cloning again. - HTTPS with an explicit username is the fallback, not the default.
It is a small thing, but writing it down saved me from rediscovering it every time I changed networks.