Specify an SSH key for git push for a given domain
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you use Git with multiple hosts or multiple accounts, you often need different SSH keys for different domains or host aliases. The clean way to do that is not inside Git itself, but in your SSH client configuration.
Git delegates SSH authentication to ssh, so the real control point is ~/.ssh/config. Once SSH knows which key to use for a given host or alias, git push follows that configuration automatically.
The Standard SSH Config Pattern
A basic host entry looks like this:
This says:
- the alias
github-workshould connect togithub.com - the SSH user is
git - the selected private key is
~/.ssh/id_ed25519_work - only that identity should be offered
Now set the repository remote to use the host alias instead of the raw domain.
After that, git push uses the key configured for github-work.
Why the Alias Matters
If you keep using [email protected]:owner/repo.git, SSH matches the plain github.com host entry. That means you cannot distinguish two different accounts for the same domain unless you introduce aliases such as github-work and github-personal.
Example:
Then different repositories can use different remotes:
or:
Test the SSH Mapping
Before debugging Git, test the SSH configuration directly.
If the alias and key are configured correctly, the host should recognize the intended account.
This is the fastest way to separate SSH configuration issues from Git remote issues.
Domain-Level Configuration Without Aliases
If you truly want one key for one domain and no special aliasing, you can configure the real host name directly.
That works when one domain maps cleanly to one identity. Aliases become necessary mainly when multiple identities share the same underlying host name.
Optional: Force a Key Per Repository Command
There are also per-command or per-repository overrides, but they are usually less maintainable than SSH config.
For example:
This works, but it is better as a temporary override than as the long-term default. The SSH config file is clearer and easier to reuse.
File Permissions and Agent Use
Your private key and config should also have reasonable permissions.
If you use an SSH agent, add the key there too if desired:
The agent is optional when the SSH config points to the correct key, but it can make repeated authentication smoother.
Common Pitfalls
One common mistake is configuring a host alias in ~/.ssh/config but leaving the Git remote pointing at the original raw domain name. In that case, the alias is never used.
Another issue is forgetting IdentitiesOnly yes. Without it, SSH may offer extra keys from the agent, which can make authentication look inconsistent.
It is also easy to test with ssh -T [email protected] when the repository actually uses the alias github-work. Test the exact host string that the Git remote uses.
Finally, keep the SSH key problem and the Git remote problem separate. Git push authentication is often correct as soon as SSH is configured correctly.
Summary
- The normal way to choose an SSH key for
git pushis through~/.ssh/config. - Use
Hostaliases when the same domain must support multiple identities. - Point the Git remote at the alias, not just the real host name.
- Test with
ssh -Tagainst the exact alias to verify the mapping. - '
GIT_SSH_COMMANDworks for temporary overrides, but SSH config is usually the cleaner long-term solution.'

