DEV Community

Shinji NAKAMATSU
Shinji NAKAMATSU Subscriber

Posted on

Use a different git config for each directory using conditional include

It is possible for git to use different config references depending on specific conditions.

This was made aware to me by a question in an earlier post. (Thanks to @francoislp asked this question).

For example, if you have a working directory cloned under the ~/work/ directory and want to use the following configuration:

Add a conditional Include to ~/.gitconfig like this:

[includeIf "gitdir:~/work/**"]
        path = ~/work/.gitconfig
Enter fullscreen mode Exit fullscreen mode

Then put the following in ~/work/.gitconfig

[user]
        name = Foo
        email = foo@example.com
Enter fullscreen mode Exit fullscreen mode

Then, for commits in the working directory under ~/work, the commit will be logged as Foo <foo@example.com>.

As follows:

contrast of execution results

See also

Top comments (0)