Manage Multiple Git Accounts: A Developer's Guide

Created: 2024-12-25  Last Updated: 2025-02-02
git commit -m "feat : add awesome feature for the organisation that I work for"
git push
git log
commit 67cd...
Author: Personal Account <personal@mowrish.dev>

Oh no... Wrong Email! - _ -

Looks familiar? We have all been there:

This is a common challenge for people who work on personal and organisation projects and, especially for freelancers who work with multiple client repositories.


The Clean Solution :

To solve this problem, we'll create a structure that organises projects into separate folders and configure Git and SSH at the folder level. This approach allows for hassle-free switching between accounts without confusion. For this example, let's take two accounts - personal and work

1. Setting up the Project Structure

First, create two folders to separate personal and work projects.

mkdir ~/personal
mkdir ~/work

2. Set up Git Configurations

In the global git configuration file - ~/.gitconfig (Create .gitconfig file if not present), include the following configuration :

# For personal account
[includeIf "gitdir:~/personal/"]
    path = ~/gitconfig/.gitconfig-personal

# For work account
[includeIf "gitdir:~/work/"]
    path = ~/gitconfig/.gitconfig-work

Now, create two separate git configuration files :

~/gitconfig/.gitconfig-personal:

[user]
   name = personal
   email = personal@mowrish.dev

~/gitconfig/.gitconfig-work:

[user]
   name = work
   email = work@mowrish.dev

This provides the flexibility to maintain git configurations for your personal projects which is different or not required for your organisation projects and vice versa. Feel free to add them in the respective file.

3. Configure Git Identities - Local

Generate separate SSH keys for personal and work accounts

ssh-keygen -t ed25519 -C "personal@mowrish.dev"
ssh-keygen -t ed25519 -C "work@mowrish.dev"

Add the keys to the global SSH configuration file - ~/.ssh/config (Create config file if not present)

# For personal account
Match exec "pwd | grep -q '$HOME/personal/'"
    HostName github.com
    User personal
    IdentityFile ~/.ssh/id_ed25519_personal

# For work account
Match exec "pwd | grep -q '$HOME/work/'"
    HostName github.com
    User work
    IdentityFile ~/.ssh/id_ed25519_work

Note : SSH reads its configuration files directly, without going through shell interpretation. So, it does not expand ~ to /Users/username which is a shell expansion capability.

This configuration ensures that each SSH key (identity) is tied to its designated folder. Additionally, the key will only be utilised for the host and user specified in the corresponding configuration block.

4. Configure Git Identities - Remote Repository

To complete the setup, you need to add the public SSH keys to your respective Git provider accounts. This step ensures that your local machine can authenticate with the remote repositories using the appropriate identity.

~/.ssh/id_ed25519_personal.pub  # For personal account
~/.ssh/id_ed25519_work.pub      # For work account

For detailed instructions specific to each Git provider, refer to their official documentation. Listed few below.

GitHub - Adding a new SSH key to your account

Bitbucket - Provide Bitbucket Cloud with your public key

GitLab - Add an SSH key to your GitLab account

5. Verify the configurations

To ensure your Git and SSH configurations are working correctly, follow these steps:

Navigate to your project folder (e.g., ~/personal) and run:

git config --list

This command will display the Git configuration specific to that folder. You should see your personal email and name listed.

From the same project folder (e.g., ~/personal), execute:

ssh -T personal@github.com

If configured correctly, you'll receive a success message from Git provider, confirming your personal account authentication.

To reconfirm that your configurations are folder-specific:

You should observe:


This one-time setup ensures that your Git and SSH configurations are correctly isolated to their respective project folders, preventing accidental use of the wrong credentials and configurations.

Happy coding, and may your commits always be associated to the right account!