Multiple Git SSH Profiles

Published on
3 mins read
––– views
Thumbnail Image


If you have multiple GitHub accounts or use various cloud Git hosting platforms and want to set them up on a single machine, this guide will show you how to achieve it using Git's includeIf feature.

How It Works

For each profile, create a corresponding folder on your machine. Git will determine which SSH profile to use based on the current folder.

Setting Up Folder Structure

For instance, if you have two GitHub profiles, dedicate two different folders to host their projects.

  • ~/Developer/work_repo/
  • ~/Developer/personal_repo/

Alternatively, you can use the default profile in all folders except, say, work_repo. In this case, create one profile (work_profile), and all other folders will use your default config.

Make sure to use the default SSH file name and not change it while creating in this case.

Ensure that you clone your projects in the correct directory so that the respective SSH profile will be used. For example, any folders inside ~Developers/work_profile/ will use your work SSH credentials.

Setting Up SSH Keys

Firstly, create different SSH keys for each account. Follow these three steps:

  1. Create a new SSH key:
ssh-keygen -t ed25519 -C ""
  1. When prompted for the file name, enter something you can associate with and make sure to enter the complete path. For example:
  1. Leave the passphrase empty by hitting the Enter key.

Repeat these steps for as many accounts as you have.

Configuring ~/.gitconfig

To instruct Git on which profile to use based on the project's folder, add the following code at the end of your .gitconfig:

[includeIf "gitdir:/path/to/work/repository/"]
    path = ~/.ssh/<profile_name>

This tells Git to use the specified profile at the given path if in the mentioned directory or its subdirectory. Please ensure you have a / at the end of the repository in gitdir; otherwise, subdirectory folders won't work.

Place this configuration at the end to avoid potential overwriting by other configs in the file.

Creating Profiles

Assuming you have a work profile and a personal profile, set up a folder for the work_profile, and let all other folders use the default profile.

Create a file named: ~/.ssh/github_work_profile

    email =
    sshCommand = "ssh -i ~/.ssh/github_work_key"

If you want to use a different email ID for work and personal projects or change the name, feel free to do so. Replace the appropriate names of SSH keys you created.

Now, in your .gitconfig, set the file path in path, for example:

[includeIf "gitdir:/path/to/work/repository/"]
    path = ~/.ssh/github_work_profile

Verifying Everything Works

Remember, this setup works only if the specified directory has a folder with Git initialized.

Check for your email in that folder:

git config

It should match the profile you want to be in. If not, make sure all the paths are correct.

If you require any help just click "Discuss on Twitter" or comment down below and I will help you out.

Happy Coding