Back to Blog

How to Use Different SSH Keys for Git Per Project

Learn how to configure multiple SSH keys for different Git repositories. Useful when you have separate GitHub/GitLab accounts for work and personal projects.

How to Use Different SSH Keys for Git Per Project

When working with multiple Git hosting accounts (e.g., a personal GitHub and a work GitLab), you need separate SSH keys for each. This guide shows you how to configure your system to automatically use the correct key for each project.

Why Use Different SSH Keys?

  • Security isolation: Keep work and personal credentials separate
  • Multiple accounts: GitHub/GitLab don't allow the same SSH key on multiple accounts
  • Organization policies: Some companies require dedicated keys for their repositories

Step 1: Generate SSH Keys

Create a separate key for each account. Use descriptive names:

# Personal GitHub key
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_github_personal

# Work GitLab key
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_gitlab_work

Tip: The -t ed25519 flag creates a modern, secure key type. Use -t rsa -b 4096 if your server doesn't support Ed25519.

Step 2: Add Public Keys to Git Hosts

Copy and add each public key to the respective platform:

# Copy personal key
cat ~/.ssh/id_ed25519_github_personal.pub
# Add this to GitHub → Settings → SSH and GPG keys

# Copy work key  
cat ~/.ssh/id_ed25519_gitlab_work.pub
# Add this to GitLab → Preferences → SSH Keys

Step 3: Configure SSH Config File

Create or edit ~/.ssh/config to define host aliases:

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

# Work GitLab
Host gitlab-work
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_ed25519_gitlab_work
    IdentitiesOnly yes

# Default GitHub (optional)
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_github_personal
    IdentitiesOnly yes

Important: The IdentitiesOnly yes directive ensures SSH only uses the specified key, not others from your agent.

Step 4: Clone Repositories Using Host Alias

When cloning, use your custom host alias instead of the actual hostname:

# Instead of:
git clone [email protected]:username/personal-repo.git

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

# For work:
git clone git@gitlab-work:company/work-repo.git

Step 5: Update Existing Repositories

For repositories already cloned, update the remote URL:

# Check current remote
git remote -v

# Update to use host alias
git remote set-url origin git@github-personal:username/repo.git

Alternative: Per-Repository Git Config

You can also configure SSH key per repository using Git config:

# Inside your repository
git config core.sshCommand "ssh -i ~/.ssh/id_ed25519_gitlab_work -F /dev/null"

Or set it globally for a directory pattern in ~/.gitconfig:

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work

Then create ~/.gitconfig-work:

[core]
    sshCommand = ssh -i ~/.ssh/id_ed25519_gitlab_work -F /dev/null
[user]
    email = [email protected]
    name = Your Work Name

Testing Your Configuration

Verify each key works correctly:

# Test personal GitHub
ssh -T git@github-personal
# Should output: Hi username! You've successfully authenticated...

# Test work GitLab
ssh -T git@gitlab-work
# Should output: Welcome to GitLab, @username!

Troubleshooting

SSH still uses wrong key

Add -v flag for verbose output:

ssh -Tv git@github-personal

Look for which identity file is being offered.

Permission denied

Ensure your keys have correct permissions:

chmod 600 ~/.ssh/id_ed25519_*
chmod 644 ~/.ssh/id_ed25519_*.pub
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config

Agent has too many keys

If ssh-agent has many keys loaded, it might try the wrong one first. The IdentitiesOnly yes in config prevents this.

Summary

  1. Generate separate keys for each account
  2. Add public keys to respective Git platforms
  3. Configure ~/.ssh/config with host aliases
  4. Use host aliases when cloning or update existing remotes
  5. Test with ssh -T to verify

This setup keeps your credentials organized and secure while seamlessly working across multiple Git accounts.