Git's git config command (hereafter referred to as "config") enables you to set repository or global options for Git. It has many options, and one of them,
includeIf, is handy when you have dual roles using Git, for example, working full time as a developer and contributing to open source projects in your spare time. Most people in this situation don't want to use a common config for both roles or, at the very least, would rather keep certain portions of their config distinct, especially if they use the same computer in both roles.
This is my situation, so I keep two separate sets of mail IDs as part of my Git config. This way, commits in repositories related to projects at my workplace carry my office mail ID, whereas the commits made to repositories hosted in my personal GitHub account use my personal mail ID.
To illustrate, here is a sample snippet from my global config (maintained at
$HOME/.gitconfig), which I will describe below.
[includeIf "gitdir:~/priv_scm/"] path = ~/priv_scm/.gitconfig [includeIf "gitdir:~/work_scm/"] path = ~/work_scm/.gitconfig
includeIf.condition.path variable, part of the
include config directives, allows you to set a custom config conditionally. Also, the path to the custom config can be set as part of the directive.
This directive supports three keywords:
onbranch. I will briefly explain
gitdir, which I use in the code snippet above; you can learn about the other two in the docs.
gitdir keyword in the
includeIf directive causes a conditional check to be performed on the pattern. Per the rule, if the current working directory matches the specified directory pattern in
gitdir, then it picks the config from the given path. I'll apply this rule on the config snippet to show how it's evaluated.
In the config snippet, you can see a simple pattern,
~/, used along with the
gitdir keyword. This pattern is substituted with the value stored in the
$HOME environment variable.
How to use it
If you're using the same system to work on open source projects hosted on GitHub or GitLab and committing to Git repositories at work, you can have two top-level directories, like
$HOME/work_scm. Within these two directories, you can have two separate
.gitconfig files with settings related to your
user.email. However, they can also be included in a global
.gitconfig stored at
$HOME that can hold all the customizations common to both environments.
Here is a snippet of an example
$ cat $HOME/priv_scm/.gitconfig [user] name = Ramanathan Muthiah email = <personal-mailid-goes-here>
With this config in place, you can switch directories and start working on open source projects without resetting some of the config related to Git manually. These changes are handled automatically in the main
.gitconfig with the aid of the
includeIf conditional directive.
I hope this tip helps you organize your Git projects. What are your favorite Git tips? Share them in the comments!
Comments are closed.