Set git config values for all child folders

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

|   ---.gitconfig_override
|   ---childFolder1
|       \---[...]
|   ---childFolder2
|       \---[...]

And I want the settings defined in .gitconfig_override to apply in childFolder1 and childFolder2.

  • Git: delete a single remote revision
  • Disable git staging area
  • Including the current branch name in the commit template
  • How can I share a git configuration?
  • How to configure git status message to not reference git push?
  • Why doesn't git pull restore files I've deleted from my working directory?
  • 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):

    • Create separate users for “work” and “play”, set their user-level settings appropriately, and log in as the appropriate user when I switch context (hassle, plus I could easily forget to switch)
    • Create a script that searches for git repos inside “workFolder”, and adds/updates their .gitconfig files to hold the appropriate details (if I create a repo and forget to run the script before pushing, I will push as the wrong person)
    • “hack” git such that every time it creates a repo, it checks the filepath and, if appropriate, updates the .gitconfig file (complicated, messy, and almost certainly The Wrong Way To Do It – plus, I wouldn’t have the first clue how to go about it!)

    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!

  • How to check the conflict of two branch, but not need to merge them?
  • Git: Pulling a rebased branch
  • Why does “git status” show way too much untracked files?
  • Git: How to return from 'detached HEAD' state
  • GIT - sparse checkout not working as expected
  • How can I rename a git repository with submodules?
  • 3 Solutions collect form web for “Set git config values for all child folders”

    EDIT: Git 2.13 introduced conditional includes, which are designed to solve this exact problem.

    My original answer is preserved below, for history’s sake (and users stuck on older versions of git).


    The exact behavior you desire is not supported, based on reading the gitconfig manpage.

    However, as of git 1.7.12, Git reads config data from four different sources, two of which are user-specific:

    $XDG_CONFIG_HOME/git/config and ~/.gitconfig. Entries in ~/.gitconfig override entries in $XDG_CONFIG_HOME/git/config.

    That means you can store your personal gitconfig at $XDG_CONFIG_HOME/git/config and put machine-specific overrides in ~/.gitconfig. Something like

        email =

    in ~/.gitconfig should cover your email case.

    Note that if $XDG_CONFIG_HOME isn’t set git will look for ~/.config/git/config.

    This works well for me, since I only ever have two personal repos on work machines (my emacs config and my dotfiles). If you add personal repos to your work machines frequently, this may not be good enough for you.

    In that case, custom wrappers around git init and git clone would be your best bet.

    Any binary on your $PATH whose name matches ‘git-*’ can be called as a git command, so you’d just need a pair of shell scripts that call the original command with all passed args, then copy the correct config file into .git/config.

    You can use direnv command to set environment variables that apply to all child folders. You can be anywhere in that directory hierarchy. If the git setting you are trying to set can be controlled by an environment variable, then you are in luck.

    Read the direnv basic page to set it up for your shell. For zsh, it was as simple as to stick this line at the bottom of my .zshrc and restart the shell.

    eval "$(direnv hook zsh)"

    Check if the git setting you want, can be controlled by an environment variable. It seems you want to control for a particular directory-tree, which is controlled by setting GIT_AUTHOR_EMAIL. Environment variables take precedence over config. A complete list of environment variables git reads is here:

    As, direnv’s page specifies, create a file .envrc at the root of the hierarchy, in your case topLevelFolder1

    For example:

    echo export > .envrc

    “Allow” the envrc: direnv allow . That’s it!

    Every time you jump into the hierarchy, direnv will find the said .envrc file and load it.

    $ cd ~/topLevelFolder1/childFolder1/project_name
    direnv: loading ../../../.envrc
    direnv: export +GIT_AUTHOR_EMAIL
    $ echo ${GIT_AUTHOR_EMAIL}

    Jump out of the dir-structure and direnv will unload those variables

    cd ~
    direnv: unloading

    The [include] section in git config (.git/config, ~/.gitconfig …) is what you are looking for.

        path = /path/to/ ; include by absolute path
        path = foo ; expand "foo" relative to the current file
        path = ~/foo ; expand "foo" in your $HOME directory

    See detailed answered question: Is it possible to include a file in your .gitconfig

    See git-config Documentation:


    Add in childFolder1/.git/config and childFolder2/.git/config:

        path = ../.gitconfig_override
    Git Baby is a git and github fan, let's start git clone.