Customize your Git config settings based on your working directory

On my personal laptop, I have a directory Development that houses all of my local code projects and cloned repositories. Inside this folder, I also have a Kong directory that serves as the parent for any local work project repositories.

I sometimes forget to switch my git configuration settings when moving between personal and work projects for things like my email address and signing key, so I created a workflow that automatically includes the desired personal or work .gitconfig settings depending on the directory I'm working in.

This setup also works with my remote dotfiles setup so I'm able to sync the settings between multiple computers, while leaving some settings local to this specific laptop.

Setup

Open your default ~/.gitconfig file from the home folder on your computer. You'll likely already have settings in this file. Identify the settings in the config file that you would like to modify depending on the working directory.

I typically need to switch my email address and commit signing key based on the project I'm working on, but you may include any valid git config settings.

The instructions below outline the setup as if for your personal laptop, whereby the default settings apply to all folders, and the "work" folder is treated as a special directory that should have different settings. If setting up on your work computer, simply swap the instructions so that the Personal settings below apply to your default work home directory, etc.

Personal settings

First, create a new file named ~/.gitconfig-local-personal in your home folder. This file will contain all of the default settings for this computer.

$ touch ~/.gitconfig-local-personal

Add the settings from your original .gitconfig that should be utilized for personal projects (or settings that should be used by default on your personal laptop).

Here's an example for customizing your email address and commit signing key:

# ~/.gitconfig-local-personal

[user]
  email = marty@mcfly.org
  signingkey = /Users/marty/.ssh/id_ed25519.pub

Work settings

Now create a file named ~/.gitconfig-local-work for your work project settings:

$ touch ~/.gitconfig-local-work

Add the same settings as the personal file, modified for your work project settings:

# ~/.gitconfig-local-work

[user]
  email = marty.mcfly@thepinheads.com
  signingkey = /Users/marty/.ssh/work_key_id_ed25519.pub

Include the correct settings based on the working directory

Now that we have our desired settings configured, let's modify the original ~/.gitconfig to include them conditionally based on the current working directory.

First, remove any settings from this ~/.gitconfig that were moved into our personal and work config files.

Next, add the include statements shown below to your ~/.gitconfig` file to automatically pull in the corresponding configuration depending on the current project's working directory.

# ~/.gitconfig

# Conditionally import the `.gitconfig-local-* file
# based on the current working directory.

# Include our personal config by default for all directories
[includeIf "gitdir:~/"]
  path = ~/.gitconfig-local-personal
# Include our work config if the project is inside the `~/Development/Work/` directory
[includeIf "gitdir:~/Development/Work/"]
  path = ~/.gitconfig-local-work

Now, when working on a project within your ~/Development/Work/ directory, your work git configuration will be automatically applied.