Juan Elosegui

Use multiple git configuration

Monday, 13 December 2021

When working with multiple git repositories for different companies or projects, it's often necessary to use different git configurations (like email and name) for each context. This is a quick guide on how to set up multiple git configurations based on repository location.

Directory Structure

First, let's organize our projects in a way that makes it easy to apply different git configurations. Here's an example structure:

C:\dev
├───Company1
│   │   .gitconfig
│   │
│   ├───Project1
│   └───Project2
└───Company2
    │   .gitconfig
    │
    ├───Project1
    └───Project2

In this setup, we have a main development directory with subdirectories for each company/context, and each company directory contains its own .gitconfig file.

Configuration Setup

1. Global Configuration

First, set up your global .gitconfig file (usually in your home directory) with default settings and include rules:

[user]
    email = email@global.com
    name = global name
[includeIf "gitdir:C:/dev/Company1/"]
    path = C:/dev/Company1/.gitconfig
[includeIf "gitdir:C:/dev/Company2/"]
    path = C:/dev/Company1/.gitconfig

2. Company-Specific Configurations

Then create separate .gitconfig files for each company:

Company1 .gitconfig:

[user]
    email = email@company1.com
    name = company1 name

Company2 .gitconfig:

[user]
    email = email@company2.com
    name = company1 name

How It Works

Git will use the global configuration by default, but when you're working in a repository under the C:/dev/Company1/ or C:/dev/Company2/ directories, it will automatically use the corresponding company-specific configuration.

This setup allows you to:

  • Keep your git configurations organized and separate
  • Automatically use the correct email and name for each company/project
  • Avoid accidentally committing with the wrong identity

Remember to use forward slashes in the paths, even on Windows, to avoid any path-related issues.

comments powered by Disqus