In this article, I show how to use Git includeIf statements to include multiple configurations based on the directory you are currently in.
We are giving away any course you need on Udemy. Any price any course.
To enter you have to do the following:
- 👍 React to this post
- ✉️ Subscribe to our newsletter
Recently I had to set up a new machine. When setting up a new environment, one of the first things is also setting up Git.
This is rather straightforward, you install Git and configure some preferences to your need like setting your name and email. You can do so by running the following commands in your terminal.
git config --global user.name "John Doe" git config --global user.email "firstname.lastname@example.org"
This will save your settings in a global
~/.gitconfig file located in your home directory.
[user] email = "email@example.com" name = "John Doe"
Of course, you could customize it even further, but let's keep it simple for now.
The issue arises, when you are working with different projects, which might belong to different accounts like for example you are using your business email to work on your daily tasks but might also want to contribute to other open-source projects, using your private email.
Since Git allows you to create a local
.gitconfig per project, you can technically set individual configs per project. Running
git config without the
--global flag will save app settings in the git config, relative to your project directory
Although this approach does work for a single project, it does not scale when switching projects.
Luckily Git has a built-in feature to source different config files based on your current directory.
According to the official documentation it does the following:
The contents of the included file are inserted immediately as if they had been found at the location of the include directive. If the value of the variable is a relative path, the path is considered to be relative to the configuration file in which the include directive was found.
This is how it works in practice. In your global
.gitconfig create two, or more,
includeif statements, which will source different configuration files based on the matching directory. Shared configuration like the user's name or aliases could still go into the global config.
# ~/.gitconfig [includeIf "gitdir:~/Documents/private/GitHub/"] path = ~/Documents/private/GitHub/.gitconfig [includeIf "gitdir:~/Documents/work/GitHub/"] path = ~/Documents/work/GitHub/.gitconfig # shared configuration like user name, aliases or colors [user] name = "John Doe"
Next, you have to create another two configs in the directories you just defined. In this example, I simply created a directory for all private and work-related projects.
. └── Documents ├── private │ └── GitHub │ └── .gitconfig └── work └── GitHub └── .gitconfig
# ~/Documents/private/GitHub/.gitconfig [user] email = "firstname.lastname@example.org" signingkey = "ABC"
# ~/Documents/work/GitHub/.gitconfig [user] email = "email@example.com" signingkey = "ZYX"
Of course, you could also add any other setting here if needed.
Thank you for reading, talk to you next time, and stay safe! 👋
Make sure to subscribe to our newsletter 💌 to receive our weekly recap directly on your email and react to this post to automatically participate in our giveaway 🎁
If you would like to join our discussions, you can find us on Discord 📣.