Customizing Git Configuration Based on Remote Repositories

When working with multiple Git repositories, such as a private workplace repository and a public GitHub repository, you might want to use different configurations for user.name and user.email. For instance, your workplace might require you to use your corporate email, while you might want to use your personal email for GitHub. In this blog post, we’ll explore how to dynamically set Git configuration based on the remote repository using Git’s includeIf directive.


Problem Overview

By default, Git applies global configurations from your ~/.gitconfig file to all repositories. However, this can become problematic when:

  • You need to use different identities for different repositories.
  • Switching between private and public repositories requires manual updates to your configuration.

Fortunately, Git provides a powerful feature for conditional configuration which automatically enables us to apply the correct configuration based on the remote repository.


Conditional Configuration Using includeIf

Git’s includeIf directive allows you to specify separate configuration files that are applied based on certain conditions. One such condition is hasconfig:remote.*.url, which matches remote URLs using glob patterns.

Here’s how we can configure Git to apply different user.name and user.email values based on the remote repository URL.

Step 1: Edit Your Global Git Configuration

Open your global Git configuration file (~/.gitconfig) in your favorite editor:

git config --global --edit

Add the following lines to include separate configurations based on the remote URL:

[includeIf "hasconfig:remote.*.url:*github.com*/**"]
    path = ~/.gitconfig-github

[includeIf "hasconfig:remote.*.url:*your-workplace.gitserver.com*/**"]
    path = ~/.gitconfig-work

Step 2: Create Separate Config Files

Create the additional configuration files that the includeIf directive references.

GitHub Configuration

Create ~/.gitconfig-github with the following content:

[user]
    name = GitHub User
    email = github.user@example.com

Workplace Configuration

Create ~/.gitconfig-work with the following content:

[user]
    name = Work User
    email = work.user@company.com

Step 3: Verify Remote URLs

To ensure that the configuration applies correctly, confirm the remote URLs of your repositories:

git remote -v

For GitHub, the URL might look like:

git@github.com:username/repo.git

For the workplace repository, it might look like:

https://your-workplace.gitserver.com/repo.git

Step 4: Test the Configuration

Navigate to a repository and verify the applied user.name and user.email:

git config user.name
git config user.email

You can also check the origin of the configuration:

git config --show-origin user.email

This will confirm which configuration file is being used.


Why *github.com*/** Works

Git interprets remote URLs hierarchically, so /** is required to match the path structure after github.com. The pattern, *github.com*/**, ensures:

  • It matches any URL containing github.com.
  • It accounts for additional components in the URL (e.g., /username/repo.git).

Similarly, for the workplace repository, *your-workplace.gitserver.com*/** ensures a proper match.


Debugging Tips

If the configuration doesn’t work as expected:

  1. Check Git Version: Ensure you are using Git 2.38 or later:
    git --version
    
  2. Check Remote URL: Verify the remote URL with:
    git remote -v
    
  3. Trace Configuration Loading: Use the following command to debug how Git applies configurations:
    GIT_TRACE=1 git config user.email
    

Conclusion

Using conditional configuration in Git can save you from the hassle of manually switching user.name and user.email for different repositories. By leveraging includeIf and patterns like *github.com*/**, you can dynamically apply the correct identity based on the remote URL.

This approach not only simplifies your workflow but also ensures you’re always using the right credentials for the right repositories.