51

我使用两种不同的 git 电子邮件,一种用于工作,另一种用于公共项目。最初我认为我可以在我所有公共存储库所在的文件夹中创建一个单独的 .gitconfig 并使用不同的电子邮件,并且 git 会尊重这一点,但唉,这似乎行不通。轻松设置类似内容的最佳方法是什么?我想避免在每个公共回购中专门更改电子邮件。

4

7 回答 7

56

自 git 2.13以来最好的方法是使用Conditional includes

一个例子(从这里的答案复制):

全局配置 ~/.gitconfig

[user]
    name = John Doe
    email = john@doe.tld

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig

工作特定配置 ~/work/.gitconfig

[user]
    email = john.doe@company.tld
于 2020-02-21T18:05:58.167 回答
8

如其他答案中所述,您无法真正为每个目录设置凭据。但是 git 允许您在每个存储库的基础上这样做。

# Require setting user.name and email per-repo
$ git config --global user.useConfigOnly true

这样,在您第一次提交时,您将收到一个错误,要求您提供姓名和电子邮件。在您的 repo 文件夹中,将您的凭据一次添加到您的 repo 中,然后您将使用此身份提交:

$ cd path/to/repo
$ git config user.name My Name
$ git config user.email mail@example.com
于 2017-04-12T09:00:01.060 回答
7

我有完全一样的问题。作为临时解决方案,我在我的.bashrc

alias git='GIT_AUTHOR_EMAIL=$(
      p=$(pwd)
      while [[ $p != "$HOME" ]]; do
        [ -e $p/.gitemail ] && cat $p/.gitemail && break
        p=$(dirname $p)
      done) GIT_COMMITTER_EMAIL=$(
      p=$(pwd)
      while [[ $p != "$HOME" ]]; do
        [ -e $p/.gitemail ] && cat $p/.gitemail && break
        p=$(dirname $p)
      done) /usr/bin/git'
alias g=git

这样我.gitemail在父目录中有两个不同的文件:

  • ~/work/.gitemail
  • ~/github/.gitemail

请注意,我只是user.email以这种方式切换,其他所有内容都集中在~/.gitconfig. 这是一个解决方案,但不是很好。

希望 StackOverflow 上的人有更好的主意...

于 2011-12-13T17:15:15.213 回答
7

切换到 ZSH 后,这是我对使用目录更改触发的“配置文件”问题的修改后的解决方案。这个解决方案的好处是它可以用于其他设置。

将其弹出到您的 zsh 配置中:

#
# 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

然后在您的 zsh git 自定义的其他地方:

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"
}
于 2011-12-27T13:18:42.037 回答
5

真正的答案是这是不可能的。

但是,感谢 @pithyless ,并且因为我已经使用自定义 'c' 函数来切换目录,然后是 auto ls,这就是我现在正在做的事情:

# 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;
}
于 2012-04-28T00:48:30.023 回答
4

有一个创建回购的集中机制。就像您路径中的一些脚本等一样,您可以这样做:

repo create -t public -n name

或类似的东西。

repo 命令(只是一个示例,与 Android 中的命令无关)将在必要的位置为您创建 repo,并读取配置文件并设置该 repo 的凭据(在该 repo 的 .git/config 中)基于类型是公共或私有等。

于 2011-12-13T18:04:18.000 回答
0

我有同样的需求,但我希望所有 .gitconfig 部分都可以被覆盖,而不仅仅是 user.email 和 user.name。

因为我没有找到任何我这样做的东西:https ://github.com/arount/recursive-gitconfig

这是当前代码,但请参阅 github 源以获取最新更新:

# Look for closest .gitconfig file in parent directories
# This file will be used as main .gitconfig file.
function __recursive_gitconfig_git {
    gitconfig_file=$(__recursive_gitconfig_closest)
    if [ "$gitconfig_file" != '' ]; then
        home="$(dirname $gitconfig_file)/"
        HOME=$home /usr/bin/git "$@"
    else
        /usr/bin/git "$@"
    fi
}

# Look for closest .gitconfig file in parents directories
function __recursive_gitconfig_closest {
    slashes=${PWD//[^\/]/}
    directory="$PWD"
    for (( n=${#slashes}; n>0; --n ))
    do
        test -e "$directory/.gitconfig" && echo "$directory/.gitconfig" && return 
    directory="$directory/.."
    done
}

alias git='__recursive_gitconfig_git'

这允许我根据我正在使用的存储库使用特定的 .gitconfig。

希望这可以帮助你们中的一些人

于 2017-01-29T14:35:29.710 回答