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.
Best Answer
Git v1.6.0 release note contains a clue :
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:
su
command-line tool can help).alias gitcommit='git commit --author=$(git config user.email)'
(ugly).~/.gitconfig
lookup throughHOME
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.