This may seem unusual, but I have multiple GitHub accounts. The separation of personal projects from work ones affords me privacy and autonomy, and I highly recommend it. For example:

  • work project notifications can be sent to a dedicated work email address while personal project notifications are sent to a personal email address
  • co-workers do not see my personal email address embedded in my commits
  • employers can enforce account-level settings (e.g. username or password restrictions) that don't impact my personal preferences for those settings
  • employers can enforce rules about my involvement – as a representative of the company  – in open-source communities without also controlling how I represent myself as an individual in those same communities

The problem, however, was that multiple GitHub accounts meant I had difficulty managing my git configs across repos. My global git config was set to my personal account, and whenever I cloned a new work repo, I had to remember to modify the local git config to point to the appropriate work account instead. Needless to say, I often forgot to make the change. Oops.

Turns out, back in version 2.13 (released in 2017), git introduced the conditional include. Suddenly, I could choose which git config to use based on the parent directory that the repo was located in! So now, this is my global git config (~/.gitconfig):

    name = <my_personal_github_username>
    email = <my_personal_email>

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig

And this is my work-specific git config (~/work/.gitconfig):

    name = <my_work_github_username>
    email = <my_work_email>

Whenever I clone a repo into the ~/work directory, git uses the settings from my work profile to override my global settings! What a cool party trick.

This isn't just useful for multiple GitHub accounts either. Individuals with only one account may have their own reasons to setup profiles like this. For example, trans individuals who haven't yet come out in a community may choose to use a different name within only that community.

Additionally, all other git config key/value pairs can be set using this conditional logic. Different SSH keys can be associated with each profile using the sshCommand key and assigning it the value ssh -i <path_to_ssh_key>.

Oh, and conditional logic isn't just for directory paths either. Try adding [includeIf "onbranch:<branch>"] for branch-specific logic instead. The possibilities are endless!

Editor's notes

The above article doesn't share alternative methods that also exist to more easily achieve the benefits associated with multiple GitHub accounts. For the curious:

  1. GitHub allows users to conceal their email address by providing a public-facing <id> address instead. Check email settings for details.
  2. GitHub allows users to send notification emails from different organizations to different addresses. Check custom routing in notification settings for details.