5

In my .zshrc file I have a chpwd function that sets the GIT_CONFIG environment variable based on the current path.

Something like this:

update_gitconfig() {
  if [[ "$(pwd | grep -Poe '^/home/user/repositories/user-A(/|$)')" != "" ]]
  then
    export GIT_CONFIG="$HOME/.gitconfig-A"
    return
  fi

  if [[ "$(pwd | grep -Poe '^/home/user/repositories/user-B(/|$)')" != "" ]]
  then
    export GIT_CONFIG="$HOME/.gitconfig-B"
    return
  fi

  export GIT_CONFIG="$HOME/.gitconfig-default"
}

update_gitconfig
chpwd_functions+=(update_gitconfig)

If I run git config --list, it displays the expected configuration under every directory:

  • Under $HOME/repositories/user-A it displays the settings from $HOME/.gitconfig-A.
  • Under $HOME/repositories/user-B it displays the settings from $HOME/.gitconfig-B.
  • Everywhere else, it displays the settings from $HOME/.gitconfig-default.

The problem begins when I run git commit; it doesn't seem to get the settings:

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'user@host.(none)')

So, the question is:

Is there any way to force git commit to get the info from the $GIT_CONFIG file?

NOTE: I know I could run git config commands in the chpwd hook to apply local settings to every repository automatically, but I'm looking for a more "elegant" way.

2

Git v1.6.0 release note contains a clue :

GIT_CONFIG, which was only documented as affecting "git config", but
actually affected all git commands, now only affects "git config".

And git v1.8.2 fixes that new behavior for git clone command which was still affected.

The workarounds I can see of interest are:

  • Use git with as many users as needed (su command-line tool can help).
  • Writing aliases like alias gitcommit='git commit --author=$(git config user.email)' (ugly).
  • Copy your configuration file into your repo local configs (any config modification must then be propagated by-hand).
  • Fake ~/.gitconfig lookup through HOME environment variable override. Ex: HOME=~/.gitconfig/user-a/ git commit should read ~/.gitconfig/user-a/.gitconfig (may have some other side effects).

Also, if you go with chpwd zsh hook, be careful to support multiple zsh running in multiple repositories.

1
0

~/.gitconfig:

[includeIf "gitdir:~/Projects/"]
    path = ~/.gitconfig_personal
[includeIf "gitdir:~/Projects/work/"]
    path = ~/.gitconfig_work

~/gitconfig_personal:

[user]
    email = user@mail.com
    name = User Name

~/gitconfig_work:

[user]
    email = user@work-mail.com
    name = User Name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.