DEV Community

enbis
enbis

Posted on

Automatically switching Git Identities and SSH Keys on the same machine

When you need to use multiple Git accounts on the same computer (to simplify for the purpose of this post let's use two accounts that we will call personal and work), you may need Git to automatically use different identities depending on which repository you are working on. Git makes this possible through conditional configuration using the includeIf directive in the .gitconfig file.

This guide explains how to set up multiple Git accounts, assign each account to a specific directory, and ensure that the correct SSH key and identity are used automatically.

Do's & Don'ts

  1. Don't define multiple [user] blocks in a single .gitconfig file.
  2. Don't use the same SSH keys for different authentications.
  3. Do avoid manual git config user.email overrides per repository.
  4. Do keep personal and work repository directories cleanly separated.
  5. Do use the includeIf directive in the .gitconfig file.

The setup I will present here is fully supported by Git and scales well if you later add more accounts.

Where to start: the gitconfig file

As written before, Git does not allow you to have multiple [user] blocks in the same .gitconfig file. The first step here is to add two more Git configuration files and call them personal and work:

  • ~/.gitconfig-personal
[user]
    name = Your Personal Name
    email = your.personal.email@example.com
Enter fullscreen mode Exit fullscreen mode
  • ~/.gitconfig-work
[user]
    name = Your Work Name
    email = your.work.email@example.com
Enter fullscreen mode Exit fullscreen mode

Next, update the .gitconfig (the global one) to load the appropriate account configuration based on the directory where the repository you are working in is located. For simplicity, let's consider your personal projects are in ~/personal directory and your work related project are in ~/work directory.

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

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work
Enter fullscreen mode Exit fullscreen mode

Important note, do not forget the path should end with / to also include subfolders.

And now the SSH keys

Every SSH key pair consists of:

  • A private key
  • A public key

Let's generate two different SSH keys: personal and work. This is possible thanks to the -f option of the ssh-keygen command.

  • ssh personal
ssh-keygen -t ed25519 -C "your_personal_email@example.com" -f ~/.ssh/id_ed25519_personal
Enter fullscreen mode Exit fullscreen mode
  • ssh work
ssh-keygen -t ed25519 -C "your_work_email@example.com" -f ~/.ssh/id_ed25519_work
Enter fullscreen mode Exit fullscreen mode

Now we have the keys, the .pub is the one that should be uploaded to GitLab/GitHub/etc, while the private one is stored locally.
One last step, we need to force Git to use a specific private key according to the directory your repo is hosted. To do that we have a custom SSH command called [core] that must be used in each gitconfig files we previously created:

  • ~/.gitconfig-personal
[user]
    name = Your Personal Name
    email = your.personal.email@example.com
[core]
    sshCommand = ssh -i ~/.ssh/id_ed25519_personal
Enter fullscreen mode Exit fullscreen mode
  • ~/.gitconfig-work
[user]
    name = Your Work Name
    email = your.work.email@example.com
[core]
    sshCommand = ssh -i ~/.ssh/id_ed25519_work
Enter fullscreen mode Exit fullscreen mode

Verifying the correct account is active

Inside each project directory, run:

git config --list
Enter fullscreen mode Exit fullscreen mode

You should see either your personal or work identity depending on the directory.

Summary

Setting up multiple Git accounts is easy and clean using Git’s conditional configuration:

  • Use one main .gitconfig with the [includeIf] to load per-account config files.
  • Define account-specific user info and [core] SSH command in separate files.
  • Generate a separate SSH keys per account for better security, traceability and clean separation.

Top comments (0)