A few weeks ago, Julia Evans published to her blog a wonderful list of popular git configuration options that she curated from the community. I was thrilled that she included one of my favourites: the includeIf option. This option is frequently overlooked by many people because they may not realise how powerful it is.

Many customers require that we create a new Github account specifically for their work, which means generating a new ssh key for pushing code back, as you can’t use the same ssh key in more than one account. Or maybe you’re using the same Github account for your personal coding projects and your customer ones, but you need to use your work email address for your work commits and your personal email for your personal commits. The includeIf option can help with both scenarios, and in very similar ways as the includeIf option allows you to include different Github configurations depending on the directory that you’re working in.

Let’s imagine that you have two customers: CustomerA and CustomerB.

CustomerA doesn’t require a different GitHub account, but they do want you to put your work email address in the commit messages, rather than your personal email address. CustomerB requires that you use a different GitHub account entirely for their projects.

Your ~/.gitconfig> file should look like this:

Copy to Clipboard

 

To tell git that you want to change the name email address on your commits for CustomerA, you want a ~/.gitconfig_CustomerA file that looks like this:

Copy to Clipboard

 

Now, whenever you are working with repositories in your ~/CustomerA/ directory, your pushes will be made with your personal account, but commits should be marked with the name and email you picked for CustomerA automatically.

CustomerB is a little different. Here, you have to use a different Github account with the ssh key you created specifically for this account. Git doesn’t allow you to specify a different ssh key, but you can substitute the entire ssh command instead.

Your ~/.gitconfig_CustomerB file looks like this:

Copy to Clipboard

 

Now, whenever you are working with repositories in the ~/CustomerB/directory, your commits are made with your CustomerB email address, and the pushes are done with the ssh key you created for your CustomerB account.

How do you check what user you’ll be committing as? I have an alias I use: git whoami, which is:

Copy to Clipboard

 

Whenever I run git whoami, I get the GIT_COMMITTER_IDENT and GIT_AUTHOR_IDENT displayed in the terminal, so I can always verify that I’m making my changes using the correct settings.

Git is a powerful tool and works well right out of the box, but digging deep into the configuration files to customise its behaviour can significantly enhance your developer experience, and reduce the friction in your workflow.

I’d love for you to try it out and let me know in the comments below how it worked for you.