Custom git identity based on path
1 min read

Custom git identity based on path

Custom git identity based on path

Long gone are the days of simple projects in singular repositories. I have recently found myself in need to specify email & user in a particular subfolder of where I keep my coding projects to reflect the emails assigned by the code owning organisation.  Here's how I went about doing just that!

Iteration one - BASH

for d in ./*/ ; do (cd "$d" && git config --local user.email 'yourEmial@yourDomain.com' && git config --local user.name 'yourName'); done

that was not optimal though as I didn't want to have to run this command any time I pull a new repository, so I dug deeper. Includes was what I found and have been using since. It allows you to include a specific .gitconfig file / setting for when a certain condition is met. There is not a terrible lot of conditions you can use in your setup, but it supports gitdir which is exactly what is required!

[includeIf "gitdir:~/Projects/${projectName}/"]
    path = ~/Projects/${projectName}/.gitconfig

Voilla! Serves the purpose, is reusable and easily configurable if you keep to some simple cloning rules.