Conditional git config
Load git config conditionally⌗
If you have ever felt annoyed by having to run git config
command over and over again, git does provide you with a way to set the config globally, simply by dropping in the command --global
flag. For example:
git config --global user.email "my@email.com"
This will apply the config globally in your environment, which is nice if you a single setup.
However, there might be times when you don’t want to use the same config for different repository or workspace. For example, you have a directory which contains all your work related repositories, where your work email and username are used, and also, you have a personal directory where you store all your personal projects in it and you want to use your personal email and username for it. Here is how you can do it:
- In your work directory, create a
.gitconfig
file, where you put your work email and username in it
[user]
email = great@company.com
name = best_employee
- In your personal projects’ directory, also create a
.gitconfig
file, and add your personal email and username
[user]
email = my@email.com
name = my_username
Now, in order to tell git
to respect your settings, in your $HOME
directory, create a .gitconfig
file if you don’t have one yet, or just open the existing one that you have, and put this to it
[includeIf "gitdir:~/Work/"]
path = ~/Work/.gitconfig
[includeIf "gitdir:~/Projects/"]
path = ~/Projects/.gitconfig
This will tel git
that whenever your in ~/Work/
or any of its sub-directories, the work config will be applied. The same goes for the personal Projects
directory.
Hope you’ll find this helpful 😄