Use different Git usernames and emails for work and play

Path specific Git-configuration
June 25, 2018 by Michael

A bit hidden away in the release notes of Git 2.13 but found by my friend Michael Vitz from INNOQ is the not so new anymore feature of conditional configuration respectively conditional includes.

Git has several levels of configuration: System wide, per user, per repository and finally, on each individual command invocation. If a values is defined on several levels, the most specific counts.

Configuration files can include other files with include and includeIf. The included files behave as if the configuration they contain had been written in the including file.

includeIf includes files conditionally. Right now, it only supports one attribute, gitdir: “The data that follows the keyword gitdir: is used as a glob pattern. If the location of the .git directory matches the pattern, the include condition is met.”

Depending on the directory your repo is in, you can pull in different configuration files. That comes in very handy to configure different a different username and email address, for example to differentiate between work and play (taken directly from the release notes):

Put this into your user specific .gitconfig:

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

And create additional .gitconfig-work and .gitconfig-play (the files can reside wherever you want, you can use a full path as well:

[user]
name = Serious Q. Programmer
email = serious.programmer@business.example.com
[user]
name = Random J. Hacker
email = rmsfan1979@example.com

Read more about git-config includes and fine more usage examples in the official documentation. I curate a list of some more or less useful git snippets here.

Did you like this article? You can invite me on a coffee ☕️ if you like.

No comments yet

Post a Comment

Your email is never published. We need your name and email address only for verifying a legitimate comment. For more information, a copy of your saved data or a request to delete any data under this address, please send a short notice to michael@simons.ac from the address you used to comment on this entry.
By entering and submitting a comment, wether with or without name or email address, you'll agree that all data you have entered including your IP address will be checked and stored for a limited time by Automattic Inc., 60 29th Street #343, San Francisco, CA 94110-4929, USA. only for the purpose of avoiding spam. You can deny further storage of your data by sending an email to support@wordpress.com, with subject “Deletion of Data stored by Akismet”.
Required fields are marked *