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 git commit with the wrong email. Git allows you to manage multiple accounts using only native features of the tool.
Here I will show how I configured my Linux and Windows environment, how I keep my personal and work projects organized, making Git automatically use the corresponding accounts and the correct SSH key for each project. No more need to use git config --local user.email ... with each new clone!
The configuration uses two features of Git and SSH:
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 (such as name and email) based on the repository path.
1: One SSH Key for Each Identity
First, let's create an SSH key for each account; this is the most important part. Do not reuse old SSH keys.
Open your terminal (on Windows, Git Bash is ideal) and run the command below, replacing <your-personal-email> with your email.
`bash
ssh-keygen -t ed25519 -C "your-personal-email@example.com"
`
When ssh-keygen asks where to save the key, do not accept the default. Use different names to avoid overwriting existing keys and to make identification easier later.
`bash
Enter file in which to save the key (/home/user/.ssh/id_rsa): /home/user/.ssh/id_ed25519_pessoal
`
Repeat the process for your work account and any others you may have:
bash
ssh-keygen -t ed25519 -C "your-work-email@example.com"
In the end, you will have several key pairs in your ~/.ssh/ directory:
-
id_ed25519_pessoalandid_ed25519_pessoal.pub -
id_ed25519_trabalhoandid_ed25519_trabalho.pub
Adding the Keys to the SSH Agent (Optional, but Recommended)
To avoid having to type your key password every time, add them to the agent. SSH:
bash
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_pessoal
ssh-add ~/.ssh/id_ed25519_trabalho
2: Configuring SSH
Now, we need to tell SSH which key to use for which server. We do this in the ~/.ssh/config file. If it doesn't exist, create it.
Add an entry for each account, as in the example below. This file defines which SSH key will be used for each connection.
`bash
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
Another example (Bitbucket)
Host bitbucket.org-freelance
HostName bitbucket.org
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_freelance
`
Host: An alias for the connection. For the main host (such asgithub.com), use the domain name itself. For secondary accounts on the same service, create an alias (e.g.,bitbucket.org-freelance).HostName: The actual server address.IdentityFile: The path to the private key that should be used for this connection.
3: Add Public Keys to Git Providers
For authentication to work, you need to upload the public part of each key (.pub) to its corresponding account on GitHub, GitLab, Bitbucket, etc.
- Copy the contents of your public key:
bash
cat ~/.ssh/id_ed25519_pessoal.pub
- Paste this content into the "SSH and GPG keys" section (or similar) in your account settings on the respective website.
4: Automatically Switching Accounts with includeIf
We'll point to the location of each key according to the corresponding account folder. This ensures that whenever you're in the folder (for example, "personal"), Git will only look for the SSH key for "personal".
Edit your global Git configuration file, located in ~/.gitconfig (Linux) or C:\Users\YourUsername\.gitconfig (Windows).
Add the includeIf block to each of your "contexts". It instructs Git: "If the project is in this directory, also include the settings from this other file".
`ini
~/.gitconfig
[user]
A default user, if preferred
name = Your Default Name
[includeIf "gitdir:~/Documents/personal/"]
path = ~/Documents/personal/.gitconfig
[includeIf "gitdir:~/Documents/work/"]
path = ~/Documents/work/.gitconfig
[core]
editor = code --wait
`
Important: The / at the end of the gitdir path is crucial. It ensures that the rule applies to all subdirectories. On Windows, use forward slashes (/) in the gitdir path, as Git understands them.
5: Create the Specific .gitconfig Files
Now, create the .gitconfig files that you referenced in the previous step. They are simple files contain only the information that should override the global configuration. These files only override the necessary settings, such as user.name and user.email.
File: ~/Documents/personal/.gitconfig
ini
[user]
name = Your Personal Name
email = your-personal-email@example.com
File: ~/Documents/work/.gitconfig
`ini
[user]
name = Your Work Name
email = your-work-email@example.com
You can even add other specific configs here, such as signingkey
signingkey = WORK_GPG_KEY
`
It's easy to handle multiple accounts
This configuration is useful when the same computer is used for personal and professional projects. This approach completely separates your identities, automates the process, and eliminates a common source of errors, reducing configuration errors during commits.
You can check which configuration is active at any time by running git config user.email inside a project directory.
Top comments (0)