Setting up Git with Path-Based Author Email Addresses

If you use Git for multiple purposes such as for client projects and open source initiatives, you may find yourself in situations where you need to use an author email address for one project, but another email one for another project.

Introducing Git’s includeIf

This is where Git’s includeIf feature in git-config comes in handy. includeIf allows you to set conditionals based on specific criterias such as repo path or active branch.

Say your client projects are at ~/client/ and your open source projects are at ~/open-source. You could set your ~/.gitconfig to look like this:

[user]
    name = Kevin Lin

[includeIf "gitdir:~/client/"]
  path = .gitconfig-client
[includeIf "gitdir:~/open-source/"]
  path = .gitconfig-open-source

Your username will always be Kevin Lin. However, depending on which repo you currently are in the rest of the git configs could be different.

Create two new files ~/.gitconfig-client and ~/.gitconfig-open-source with the following layout:

[user]
  email = username@example.com

Be sure to change the email address to be different for each config files.

Now, if you go to any repo in your client directory you will see that the author email will be different than the one in any of your open source repos.

You can verify that by doing a grep of the git configs in any repo:

$ git config -l | grep user
user.name=Kevin Lin
user.email=username@example.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top