The situation is as follows:
- I have multiple domains in which I write code, e.g. professional and free time. Those are represented by different directories on my computer.
- Each of those domains contains multiple Git repositories, in a hierarchical directory structure inside one of the domain directories.
Per domain, I want to use a different email address as part of author/committer information. That is, I want my private address to be listed in my free-time projects and my company address in my professional ones.
git config
knows 3 scopes: repository, global and system-wide. What I basically need is a 4th scope between repository and global, representing a group of repositories (or simply a directory in the file system).
It seems like git config
doesn't allow that. Of course I could set the email address per repository, but I want to avoid this manual step every time I set up or clone a repository. One option would be to write a script that wraps git init/clone
and git config
, are there other ideas?
As a simple solution, use something like autoenv (sorry for the self-advertising) to set your mail address with environment variables:
based on https://stackoverflow.com/a/44036640/2377961 i think I found a way how it could work.
first you create different config files for your "custom-scopes" (e.g. professional, freetime, ect.) and add your desired user-config for each scope
.
.
than you add lines like this in your standard gitconfig:
The directories after "gitdir/i" should match the parents directory of your project groups. In my example you should store your git-repos for freetime-domains e.g. "c:/freetime/my-freetime.domain/.git"
The solution I came up with is inspired from Scott Weldon's answer. Since it was not directly applicable for my case, I adapted the hook's bash script and improved several parts*.
Assume the following directory structure from the home directory:
Initially I let Git know where my template directory is. On Windows, you may need to specify the absolute path instead (
C:/Users/MyUser/.git-template
).In
~/MyDomain/.gitconfig
I store the configuration for that directory (domain), which should be applied to all repositories inside it and its subdirectories.The interesting part is the
post-checkout
bash script, which defines the post-checkout hook. I used a customuser.inferredConfig
flag to execute it only once (ongit clone
), not repeatedly (ongit checkout
). It would of course also be possible to create a separate file to represent that state.*: The changes from the original code include:
.gitconfig
correctly (don't read lines withfor
, iterate withwhile read
instead).gitconfig
files from root to local directory, not vice-versagit clone