Managing Multiple Git SSH Keys by Directory

Managing Multiple Git SSH Keys by Directory

Infrastructure, Ansible, AWS, Home Lab

In the last post, we configured Git to automatically use different user names and emails depending on the project directory.
That solved one half of the problem.

But if you use multiple Git accounts — say, one for work, one for personal projects — you’ll also need to make sure Git connects using the right SSH key for each.

Let’s fix that next.


The Problem

By default, Git uses your system-wide SSH key (typically ~/.ssh/id_rsa or ~/.ssh/id_ed25519).
If both your personal and work GitHub accounts use SSH, you can easily run into:

ERROR: Permission denied (publickey)

or worse — commits showing up under the wrong GitHub account.

The trick is to have separate SSH keys and tell your SSH client when to use which one.


🧰 Step 1 – Generate Your Keys

If you don’t already have separate keys, create them now.

# Personal key
ssh-keygen -t ed25519 -f ~/.ssh/id_rsa_personal -C "personal@example.com"

# Work key
ssh-keygen -t ed25519 -f ~/.ssh/id_rsa_work -C "work@example.com"

You’ll end up with two key pairs:

  • id_ed25519_personal / id_ed25519_personal.pub
  • id_ed25519_work / id_ed25519_work.pub

🧩 Step 2 – Add Keys to Your Git Hosts

Add each public key (.pub file) to the correct account:

  • Personal GitHubSettings → SSH and GPG keys → New SSH key
  • Work GitHub / GitLabSame path, different account

Give them clear names like “Laptop Personal” and “Laptop Work.”


⚙️ Step 3 – Create a Custom SSH Config

Open (or create) ~/.ssh/config and define custom host aliases for each account:

# Personal GitHub
Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal
    IdentitiesOnly yes

# Work GitHub
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_work
    IdentitiesOnly yes

# Another Work Bitbucket
Host bitbucket
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/id_rsa_work_bitbucket
    IdentitiesOnly yes

Now, instead of using git@github.com, you’ll use either:

  • git@github-personal
  • git@github-work

Each alias tells SSH which key to use. This will work for any provider, not just GitHub such as GitLab or Bitbucket etc.


🧱 Step 4 – Clone Repositories with the Right Alias

When cloning, use the correct alias:

# Personal project
git clone git@github-personal:username/personal-repo.git

# Work project
git clone git@github-work:company/work-repo.git

Git doesn’t care about the alias — it’s just an SSH shortcut that ensures the correct key gets used.

If you’ve already cloned a repo, you can edit the .git/config file inside it:

[remote "origin"]
    url = git@github-work:company/work-repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

🧠 Step 5 – Combine with Conditional Git Configs

If you followed the previous post, you can now combine the two setups:

  • Use includeIf in ~/.gitconfig to pick the right author details.
  • Use SSH aliases in ~/.ssh/config to pick the right authentication key.

That means everything just works when you’re in the right directory — no more switching accounts manually.


🧩 Example Directory Setup

~/projects/personal/     → uses github-personal + personal identity  
~/work/                  → uses github-work + work identity  

Each repo inside those folders automatically gets the right author and the right SSH key.


✅ Summary

With a few lines of config, you’ve now built a clean, automatic Git environment that handles multiple accounts seamlessly.

Benefits:

  • Separate identities and SSH keys per project type
  • No more “wrong account” commits or permission errors
  • Clean, organized Git and SSH setups that just work

This approach is especially useful if you:

  • Contribute to open-source projects with your personal account
  • Work for multiple clients
  • Keep your personal and professional Git presence strictly separated

In Part 3 we go a step further and verifying our commits providing confidence they are from trusted authors