Hierarchical git config

On my personal machine, I set my personal email address in my global git configuration.

$ git config --global --get user.email
steve@personal.com

However, I also have my company code, and so I need to configure git to my company email address.

$ cd corp/project
$ git config --local --get user.email
steve@corp.com

Sometimes, however, when cloning a repo, I forget to redefine my email address, and therefore I record the use of my personal email address.

It would be possible to delete my global git configuration, thereby preventing me from making any repos before installing user.emailin the local git configuration.

This is a bit of lavash, though, and in an ideal world, I could set up a hierarchical git configuration so that repositories in a specific subdirectory (or some other ways to develop which configuration are applicable) use the most specific installation in it.

Something like the following:

~/
|
+--- .gitconfig               # sets personal email address
|
+--- src/
     |
     +--- project/            # ~/.gitconfig email address applies 
     |    
     +--- corp/
          |
          +--- .git/config    # sets corp email address
          |
          +--- project/       # corp/.git/config email address applies

AFAIK is currently not possible initially with git, this will require a new level of configuration, which is between global and local

Is there a way to achieve what I'm looking for here?

+4
source share
5 answers

It is not yet supported, however with git 2.8 (March '16) you can disable the global user configuration as follows:

git config --global user.useConfigOnly true

, git , . script .

+4

git hooks Orr Sella, . :

EMAIL=$(git config user.email)
if [ -z "$EMAIL" ]; then
    # user.email is empty
    echo "ERROR: [pre-commit hook] Aborting commit because user.email is missing. Configure user.email for this repository by running: '$ git config user.email name@example.com'. Make sure not to configure globally and use the correct email."
    exit 1
else
    # user.email is not empty
    exit 0
fi

. , , ( grep even), , . -

EMAIL = $(grep user.email $GIT_DIR/config)
if [[ $? ]]; then 
    EMAIL = $(git config user.email)
    exit 0
fi
EMAIL = $(EMAIL##*user.email* )

GIT_DIR hook.

+2

AFAIK git , per-repo .

, cd- zsh:

# ~/.zshrc
# call this function after cd-ing into a directory
__zsh-on-cd () {
if git ls-files &>/dev/null ; then
    if [[ "$PWD" =~ 'Company' ]]; then
        echo "setting git to use company identity"
        git config user.name "John Doe"
        git config user.email "doe@company.com"
    else
        echo "setting git to use personal identity"
        git config user.name "johndoes"
        git config user.email "me@personal.domain"
    fi
fi
}

chpwd_functions=(${chpwd_functions[@]} "__zsh-on-cd")
+1

, git . [1]

, script, / . (git) (, git myinit | git myclone), , , .

0

git hooks: pre-commit .

# A git hook to make sure user.email and user.mail in repository  exists before committing

repository_email=$(git config --local --get user.email)
repository_name=$(git config --local --get user.name)

if [ -z "$repository_email" ] || [ -z "$repository_name" ]; then
    # user.email is empty
    echo "ERROR: [pre-commit hook] Aborting commit because user.email or user.name is missing. Configure them for this repository."
    exit 1
else  
    # user.email is not empty
    exit 0
fi 
0

Source: https://habr.com/ru/post/1676047/


All Articles