Multiple Git Profiles

Curtis Lusmore

If like me, you need to make Git commits from multiple email addresses on the same machine (say, from your personal and your work address), here’s a handy trick that will help make sure you make every commit with the right address.

Start by creating a directory for each profile, named after the profile. In my case, I have D:/git/home and D:/git/work. Move all your Git repositories into the directory corresponding to the profile that you want to use when making commits to that repository. At the root of your profile directories, create a .gitconfig file with the following content.

[user]
  email = your.email@address.com

Obviously, use the correct email address for each profile. Now remove the email config from your global .gitconfig file, which is usually in your user’s home directory. If you’re not sure where it is, you can run

git config --show-origin user.email

to check. Once you’ve removed it, run

git config --get user.email

to confirm that it’s unset. Now add the following sections to your global .gitconfig file.

[includeIf "gitdir:D:/git/home/"]
  path = D:/git/home/.gitconfig

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

Replace D:/git/home/ with the paths to the directories you created earlier. Git will now pull in these config files whenever you’re working in a repository inside those directories.

You can test this by navigating to a Git repository inside one of your directories and running

git config --get user.email

but note that you need to be inside a Git repository, not just the root directory—Git won’t pull in the conditional configuration files outside of a Git repository.

Technically, you don’t need to remove your email configuration from the global .gitconfig file—as long as the includeIf blocks appear below your email configuration, they will take precedent. However, I prefer to remove it to make it clearer that the value there isn’t used, and to force me to put all of my Git repositories inside one of my profile directories—attempting to commit to a repository outside those directories will fail because no email address is configured.

And that’s it—never make a commit from the wrong email address again!