I know that it's possible to set per-repo configs which override the user-level config (i.e. /path/to/my/repo/.gitconfig
overrides ~/.gitconfig
). Is it possible to set git configs which override the user-level settings for all child folders of a given folder? I.e., I have
|--topLevelFolder1 |--\ | ---.gitconfig_override |--\ | ---childFolder1 | \---[...] |--\ | ---childFolder2 | \---[...]
And I want the settings defined in .gitconfig_override
to apply in childFolder1
and childFolder2
.
My motivation for this is as follows: I have a work laptop which I also use in my spare time for personal projects. All my work code is nested within a single folder. When I push to work git repos, I need to do so with my work persona - work login instead of name, and work email. When I push to my own personal (github) repos, I want to do so with my real name and personal email.
Other possible solutions I've thought of (and problems):
I checked this question, which only seems to contain solutions for single repos, not multiple. Hopefully someone will see this question who missed that one!
Git stores all global configurations in . gitconfig file, which is located in your home directory. To set these configuration values as global, add the --global option, and if you omit --global option, then your configurations are specific for the current Git repository. You can also set up system wide configuration.
Show global git config settings But at runtime, only the value set locally is used. If you would like to delete or edit a Git config value manually with a text editor, you can find the Git config file locations through the Git config list command's –show-origin switch.
# There are 3 levels of git config; project, global and system.
As mentioned by NateEag's edit, git's Conditional Includes are perfect for this. Since that answer's the one for people on git < 2.13, here's one for those who have newer versions.
First, create a new config file somewhere with the settings you want to take effect in the sub-folders - using the original question's folders, let's say it's at ~/topLevelFolder1/.gitconfig_include
In ~/.gitconfig
, add:
[includeIf "gitdir:~/toplevelFolder1/"] path = ~/topLevelFolder1/.gitconfig_include
Any subfolder of ~/topLevelFolder1
will now include the config in ~/toplevelFolder1/.gitconfig_include
- there isn't a need to manually change the .git/config
in each subfolder's repo. (This doesn't override whatever's in the subfolder config - it just adds to it, as "include" implies.)
Notes:
~/.gitconfig
because includeIf
will be overridden again by any config that comes after it./
) in the gitdir
condition is important.git config --list
is good for testing this. You'll see any overrides below includeIf
lines in the output. You can also check specific entries with, e.g., git config --get user.email
~/
and absolute paths with the Windows-style drive, like C:/
using forward slashes only. Backslashes and Unix-style mount points like /c/
don't work. Furthermore, in the includeIf
part, you must specify the path with the correct case as the comparisons are case sensitive.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With