Alex Rivera | Logout

Different .gitconfig for a given subdirectory?

Asked 2011-12-01T05:46:28.287
80

I use two different git emails, one for work and one for public projects. Initially I thought that I could create a separate .gitconfig with a different email in a folder where all my public repos are in, and that git would respect that, but alas it seems that doesn't work. What's the best way to easily setup something similar? I want to avoid having to specifically change the email in each public repo.

Edit
Report

3 Answers

7

Having switched over to ZSH, this is my revised solution to the problem using "profiles" triggered on directory changes. The nice thing about this solution is that it can be used for other settings.

Pop this into your zsh config:

#
# Thanks to: Michael Prokop. 
# More documentation: 
# http://git.grml.org/?p=grml-etc-core.git;f=etc/zsh/zshrc;hb=HEAD#l1120
#
CHPWD_PROFILE='default'
function chpwd_profiles() {
    local -x profile

    zstyle -s ":chpwd:profiles:${PWD}" profile profile || profile='default'
    if (( ${+functions[chpwd_profile_$profile]} )) ; then
        chpwd_profile_${profile}
    fi

    CHPWD_PROFILE="${profile}"
    return 0
}
chpwd_functions=( ${chpwd_functions} chpwd_profiles )

chpwd_profile_default # run DEFAULT profile automatically

And then elsewhere in your zsh git customizations:

zstyle ':chpwd:profiles:/home/user/work(|/|/*)'  profile work
zstyle ':chpwd:profiles:/home/user/fun(|/|/*)'   profile fun

# configuration for profile 'default':
chpwd_profile_default()
{
  [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
  print "chpwd(): Switching to profile: default"

  export GIT_AUTHOR_EMAIL="default@example.com"
  export GIT_COMMITTER_EMAIL="default@example.com"
}

# configuration for profile 'fun':
chpwd_profile_fun()
{
  [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
  print "chpwd(): Switching to profile: $profile"

  export GIT_AUTHOR_EMAIL="fun@example.com"
  export GIT_COMMITTER_EMAIL="fun@example.com"
}

# configuration for profile 'work':
chpwd_profile_work()
{
  [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
  print "chpwd(): Switching to profile: $profile"

  export GIT_AUTHOR_EMAIL="work@example.com"
  export GIT_COMMITTER_EMAIL="work@example.com"
}
answered 2011-12-27T13:18:42.037
4

Have a centralized mechanism for creating repos. Like some script in your path etc. whereby you do:

repo create -t public -n name

or something like that.

The repo command ( just an example, nothing to do with the one from Android ) will create the repo for you in the necessary location and read a config file and set the credentials for that repo (in the .git/config for that repo ) based on the type being public or private etc.

answered 2011-12-13T18:04:18.000
3

The real answer is that its impossible.

However, thanks to @pithyless , and because I was already using a custom 'c' function to switch directories followed by an auto ls, this is what I'm doing now:

# cd + ls, and change git email when in personal projects folder 
function c {
  if [[ "`abspath $1`" == *"$HOME/Projects"* ]]; then
    export GIT_AUTHOR_EMAIL="personal@gmail.com"
  else
    export GIT_AUTHOR_EMAIL="me@work.com"
  fi
  cd "${@:-$HOME}" && ls;
}
answered 2012-04-28T00:48:30.023

Your Answer