logo
podcast Podcast
get help Get Unstuck

How to use different SSH keys for different git repos

Just two simple steps

Joel Clermont
Joel Clermont
2025-07-21

Let's say you have two user accounts on a hosted git service, like Bitbucket or GitHub. One is your personal account and the other is your work account, and each of these accounts has its own SSH key.

When you interact with those repos over SSH, if it sends the wrong key first, the service will accept it, authenticate you as the wrong user, and then prevent access to the repo.

So how can you make sure it only sends the one specific key you want on a repo-by-repo basis? It's a two-step solution.

First, you need to set up your SSH config file for each of the accounts.

For example, here's how you might set up your ~/.ssh/config file:

Host personal-bitbucket
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/personal_id_rsa
    IdentitiesOnly yes
    
Host work-bitbucket
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/work_id_rsa
    IdentitiesOnly yes

This creates an SSH alias for each of your accounts, even though they are both on the same HTTP host.

The final line IdentitiesOnly yes ensures that only the specified key is used for authentication, preventing SSH from trying other keys.

Next, when cloning the repo, instead of using the full URL like this:

git clone [email protected]:username/repo.git

You would use the alias you set up in your SSH config like this:

git clone personal-bitbucket:username/repo.git

Notice how we can just specify the alias personal-bitbucket instead of the more typical [email protected]. The SSH config will resolve this to the correct username, host, and key automatically.

Here to help,

Joel

P.S. Would you like to join the Mastering Laravel community? It's a great investment in your career.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.