Managing Multiple Git Accounts: SSH Keys & Conditional Includes
Every developer who has ever dealt with a personal project, a job, and perhaps a freelance project on the same machine knows the pain of accidentally performing a git commit with the wrong email. Git allows you to manage multiple accounts using only native features.
This guide shows how to configure Linux and Windows environments to keep personal and work projects organized, making Git automatically use the corresponding accounts and the correct SSH key for each project—eliminating the need for git config --local user.email with every new clone.
The configuration relies on two features:
- Multiple SSH Keys: A unique key for each Git provider (GitHub, GitLab, etc.) for authentication.
-
Git Conditional Inclusion: The
includeIfdirective in your.gitconfigfile loads specific settings (name and email) based on the repository path.
Step 1: Create One SSH Key for Each Identity
First, create a unique SSH key for each account. Do not reuse old SSH keys.
1. Generate the Personal Key
Open your terminal and run the following command (replacing the placeholder with your personal email):
ssh-keygen -t ed25519 -C "your-personal-email@example.com"
When asked where to save the key, do not accept the default. Use a descriptive name:
Enter file in which to save the key: /home/user/.ssh/id_ed25519_pessoal
2. Generate the Work Key
Repeat the process for your work account:
ssh-keygen -t ed25519 -C "your-work-email@example.com"
Save it as: /home/user/.ssh/id_ed25519_trabalho
3. Add Keys to the SSH Agent (Recommended)
To avoid typing your password every time, add them to the agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_pessoal
ssh-add ~/.ssh/id_ed25519_trabalho
Step 2: Configure the SSH Config File
Tell SSH which key to use for each server by editing the ~/.ssh/config file. Add an entry for each account:
# Personal Account (GitHub)
Host github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_personal
# Work Account (GitLab)
Host gitlab.company.com
HostName gitlab.company.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_work
# Freelance Example (Bitbucket)
Host bitbucket.org-freelance
HostName bitbucket.org
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_freelance
- Host: An alias for the connection.
- HostName: The actual server address.
- IdentityFile: The path to the private key.
Step 3: Add Public Keys to Git Providers
Upload the public part of each key (.pub) to your Git provider (GitHub, GitLab, etc.):
- Copy the public key content:
cat ~/.ssh/id_ed25519_pessoal.pub
- Paste it into the "SSH and GPG keys" section of your account settings on the provider's website.
Step 4: Automate Account Switching with includeIf
Edit your global Git configuration file (~/.gitconfig on Linux or C:\Users\Username\.gitconfig on Windows). Use the includeIf block to load specific settings based on the directory:
# ~/.gitconfig
[user]
name = Your Default Name
[includeIf "gitdir:~/Documents/personal/"]
path = ~/Documents/personal/.gitconfig
[includeIf "gitdir:~/Documents/work/"]
path = ~/Documents/work/.gitconfig
[core]
editor = code --wait
Note: The / at the end of the gitdir path is crucial. On Windows, use forward slashes (/).
Step 5: Create Specific .gitconfig Files
Create the local .gitconfig files referenced in Step 4. These files should only contain the information that overrides the global configuration.
File: ~/Documents/personal/.gitconfig
[user]
name = Your Personal Name
email = your-personal-email@example.com
File: ~/Documents/work/.gitconfig
[user]
name = Your Work Name
email = your-work-email@example.com
# Optional: signingkey = WORK_GPG_KEY
Conclusion
This approach separates your identities and eliminates common errors when the same computer is used for multiple contexts. You can verify which configuration is active at any time by running:
git config user.email
Top comments (0)