Project-level git config
Background
When setting options using git config, you can set options on the global level or on the repository level. The global
configuration is typically stored in ~/.gitconfig and the repository configuration in $REPOSITORY_PATH/.git/config.
Here’s the user section of my ~/.gitconfig:
[user]
email = andre@laszlo.nu
name = André Laszlo
This configures the default name and email that will be used when I make commits in a repository with no user.email or
user.name set on the repository level.
The problem
Here’s my problem: I have a lot of repositories, and I use several email addresses.
For work, I’d like to use my work email address. I could run git config user.email "andre.laszlo@example.com", but I’d
have to remember to do it in all of the repositories I use for work - and it’s typically a lot. I could also change the
default, and remember to update the configuration for all non-work repositories, but it’s also a lot.
If you have separate work computers and private computers, you can of course just change your global configuration, but if you don’t have separate computers — or if you want to use different configurations for other reasons — this can get a bit annoying.
The solution
I recently found a really nice way to solve this by setting a configuration for many repos at once, logically adding a level between the global level and the repository level configs:
- Put all the repos in a common parent directory, let’s say
~/work/. -
Create a new
~/work/.gitconfigfile and add any overrides there, for example:[user] email = andre.laszlo@example.com -
Add this to your global git config (typically in
~/.gitconfig):[includeIf "gitdir:~/work/"] path = ~/work/.gitconfig
This means that any repository in the ~/work/ directory will get this configuration, and you don’t need to remember to
add an override to every repository.
Another nice thing is that you can add a project-level .gitignore as well, by adding this to your ~/work/.gitconfig:
[core]
excludesfile = ~/work/.gitignore