Managing Git Configuration Across Multiple Projects

Close-up of files and file folders on a desk top in an office.

Your digital product needs more than just great code, it needs intelligence. We help companies integrate AI and machine learning to create smarter, faster, and more adaptive apps and sites. Let’s talk about your vision.

Software Engineers often work on multiple projects concurrently, especially at a digital product consultancy such as DockYard. These projects often come with distinct requirements for managing the configuration of a Github repository. One of the most consistent requirements is to make commits with a specific identity:

  • Open Source: Personal email addresses are typically used for open-source contributions.
  • Employer: Company email addresses are mandatory for work-related commits.
  • Clients: Client projects may require the use of a client-specific designated email address.

Manually switching between these identities is inefficient and prone to errors. Fortunately, Git offers a built-in mechanism to address this.

Conditional Configuration with includeIf

The includeIf directive within your ~/.gitconfig file enables conditional inclusion of other configuration files based on the current directory. This allows for automatic overrides of your Git configuration based on the project context.

Implementation:

  1. Create Context-Specific Config Files: Create separate .gitconfig files for each context (personal, employer, client). These can be located anywhere but are most commonly located in the $HOME directory or in the specific context directory. For example:

    • ~/projects/personal/.gitconfig:
    [user]
        email = personal@example.com
    
    • ~/projects/employer/.gitconfig:
    [user]
        email = employer@example.com
    
    • ~/projects/client/.gitconfig:
    [user]
        email = client@example.com
    
  2. Configure Your Main ~/.gitconfig: Integrate the includeIf directives into your primary ~/.gitconfig file:

    [user]
        email = default@example.com; default user email address (overridden when conditions are met)
        name = Your Name
    
    # Place these at the bottom of `~/.gitconfig` so they override previous settings
    [includeIf "gitdir:~/projects/personal/"]
        path = ~/projects/personal/.gitconfig
    [includeIf "gitdir:~/projects/employer/"]
        path = ~/projects/employer/.gitconfig
    [includeIf "gitdir:~/projects/client/"]
        path = ~/projects/client/.gitconfig
    

With this configuration, locating project code in any subdirectory of a context will automatically activate the corresponding Git configuration overrides. Outside these designated directories, Git will default to just the settings in ~/.gitconfig.

Conclusion

The includeIf directive provides a concise and effective method for managing Git configuration, ensuring consistency and accuracy across diverse project contexts.

Newsletter

Stay in the Know

Get the latest news and insights on Elixir, Phoenix, machine learning, product strategy, and more—delivered straight to your inbox.

Narwin holding a press release sheet while opening the DockYard brand kit box