【问题标题】:Different .gitconfig for a given subdirectory?给定子目录的不同.gitconfig?
【发布时间】:2012-01-10 08:17:50
【问题描述】:

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

【问题讨论】:

标签: git email git-config


【解决方案1】:

我也有同样的问题。作为临时解决方案,我的.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 上的人有更好的主意...

【讨论】:

  • 这很好用,即使使用defunkt/hub。我刚刚将路径 /usr/bin/git 更改为 hub 并且它工作了^_^
  • 这段代码存在无限循环。更糟糕的是,如果您使用的是调用 git 的提示符,如果您在主目录之外,当前会在 所有交互式登录 shell 中导致 无限循环!我已经创建了一个固定版本in this gist,它作为一个函数工作并且相当简化了别名定义。
  • 是的,回头看,我发布的 bash 代码并不是最好的解决方案。 :) 虽然@TrinitronX bash 脚本可能更简洁,但我真的很喜欢 zsh 的配置文件如何提供更简洁的解决方案:stackoverflow.com/a/8645101/145754
  • 我没有尝试过zsh,但它似乎风靡一时。我仍然喜欢纯 bash,因为它迫使我编写通常在其他 *nix 平台上开箱即用的脚本。这篇文章的更新提醒我再看一遍,并意识到脚本中还有其他一些边缘情况可能导致无限循环(在 dirname 结果中带有空格、空或 '.' 的目录)。我已经更新了the gist script 来修复它们。
【解决方案2】:

有一个创建存储库的集中机制。就像你的路径中的一些脚本等你这样做:

repo create -t public -n name

或类似的东西。

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

【讨论】:

    【解决方案3】:

    切换到 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"
    }
    

    【讨论】:

    • elsewhere in your zsh git customizations: 是什么意思?除了~/.zshrc,我还需要单独的文件吗
    • 我刚刚将它添加到默认 ~/.zshrc 并且它起作用了。
    【解决方案4】:

    真正的答案是不可能的。

    不过,感谢@pithyless,并且因为我已经使用自定义的'c'函数来切换目录,然后是自动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;
    }
    

    【讨论】:

    【解决方案5】:

    我有同样的需求,但我希望所有 .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,具体取决于我正在使用的存储库。

    希望对大家有所帮助

    【讨论】:

      【解决方案6】:

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

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

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

      $ cd path/to/repo
      $ git config user.name My Name
      $ git config user.email mail@example.com
      

      【讨论】:

      • 这应该是答案。但是,它requires Git 2.8 才能工作。
      • 不幸的是,这会重复命令。如果我有一个文件夹 ~/job 并且我计划在下周左右在其下创建 100 个新项目,那么我必须一遍又一遍地输入我的名字等 100 次。如果我能为 ~/job 做一次会更好,它适用于所有存在的或我将来可能创建的子文件夹。
      【解决方案7】:

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

      一个例子(复制自答案here):

      全局配置 ~/.gitconfig

      [user]
          name = John Doe
          email = john@doe.tld
      
      [includeIf "gitdir:~/work/"]
          path = ~/work/.gitconfig
      

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

      [user]
          email = john.doe@company.tld
      

      【讨论】:

      • 这比其他答案中的外壳自定义要整洁得多,谢谢。
      • 这可能很明显但仍然很有趣:当将多个 git 存储库分组到一个不是 git 存储库的目录之外时,效果将仅在 git 存储库中可见,而在父目录中不可见。跨度>
      • 我也可以在 includeIf 中使用通配符来为任何子目录使用配置吗?我想在文件夹中组织 repos,而不是直接在 ~/work/ 中检出它们,但可能在 ~/work/projects/ 或 ~/work/papers 中检出
      • Conditional includes 非常强大。上面的示例在这种情况下应该可以工作,因为 gitdir 以 / 结尾:If the pattern ends with /, ** will be automatically added. For example, the pattern foo/ becomes foo/**. In other words, it matches "foo" and everything inside, recursively.
      • @chronicc 谢谢!这对我来说并不明显,你的评论让我有些沮丧。
      猜你喜欢
      • 2021-12-30
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      • 2011-02-01
      • 2016-08-17
      • 2019-04-29
      • 2020-08-19
      • 2012-04-02
      相关资源
      最近更新 更多