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
- Don't define multiple
[user]blocks in a single .gitconfig file. - Don't use the same SSH keys for different authentications.
- Do avoid manual
git config user.emailoverrides per repository. - Do keep personal and work repository directories cleanly separated.
- Do use the
includeIfdirective 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
-
~/.gitconfig-work
[user]
name = Your Work Name
email = your.work.email@example.com
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
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
-
ssh work
ssh-keygen -t ed25519 -C "your_work_email@example.com" -f ~/.ssh/id_ed25519_work
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
-
~/.gitconfig-work
[user]
name = Your Work Name
email = your.work.email@example.com
[core]
sshCommand = ssh -i ~/.ssh/id_ed25519_work
Verifying the correct account is active
Inside each project directory, run:
git config --list
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
.gitconfigwith 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)