← Blog

Managing Multiple Git Accounts (Work and Personal)

DanilloGomes · · 4 min read

Every developer who has ever juggled a personal project, a job, and maybe a freelance project knows the pain: the accidental git commit with the wrong email. Using different credentials for each "identity" can be a hassle, but Git offers an elegant and automatic solution.

This guide will show you how to configure your environment on Linux and Windows so that Git automatically uses the correct account and SSH key based on your project directory. No more git config --local user.email ... with every new clone!

The strategy is based on two powerful features:

  1. Multiple SSH Keys: A unique key for each Git provider (GitHub, GitLab, etc.) for authentication.
  2. Git Conditional Inclusion: The includeIf directive in your .gitconfig file loads specific settings (such as name and email) based on the repository path.

Step 1: One SSH Key for Each Identity

First, let's generate a unique SSH key pair for each account you want to manage.

Open your terminal (on Windows, Git Bash is ideal) and run the command below, replacing <your-personal-email> with your email.

ssh-keygen -t ed25519 -C "your-personal-email@example.com"

When ssh-keygen asks where to save the key, do not accept the default. Provide a unique filename.

Enter file in which to save the key (/home/user/.ssh/id_rsa): /home/user/.ssh/id_ed25519_personal

Repeat the process for your work account and any others you may have:

ssh-keygen -t ed25519 -C "your-work-email@example.com"

At the end, you will have several key pairs in your ~/.ssh/ directory:

  • id_ed25519_personal and id_ed25519_personal.pub
  • id_ed25519_work and id_ed25519_work.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 SSH agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_work

Step 2: Configuring the SSH "Gatekeeper"

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 acts as a "gatekeeper," directing connections to the correct identity.

# 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 as github.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.

Step 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.

  1. Copy the contents of your public key:
cat ~/.ssh/id_ed25519_personal.pub
  1. Paste this content into the "SSH and GPG keys" section (or similar) in your account settings on the respective site.

Step 4: Automatically Switching Badges with includeIf

This is the final magic. Let's tell Git to use different settings based on the directory where the project is located.

Edit your global Git configuration file, which is 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".

# ~/.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

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.

Step 5: Create the Specific .gitconfig Files

Now, create the .gitconfig files you referenced in the previous step. These are simple files that contain only the information that should override 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

# You can even add other specific configs here, such as signingkey
# signingkey = WORK_GPG_KEY

Conclusion: The Magic in Action

Done! The configuration is complete.

  • When you clone or initialize a repository in ~/Documents/personal/, Git will automatically use your personal name and email.
  • When you do the same in ~/Documents/work/, Git will use your work credentials.

You can check which configuration is active at any time by running git config user.email within a project directory.

This approach completely separates your identities, automates the process, and eliminates a common source of errors, allowing you to focus on what really matters: the code.

Tags: git,