在我的~/。gitconfig,我列出我的个人电子邮件地址下[用户],因为这是我想用于Github回购。

但是,我最近也开始在工作中使用git。我公司的git回购允许我提交,但是当它发出新的变更集通知时,它说它们来自匿名,因为它不识别我的.gitconfig中的电子邮件地址——至少,这是我的理论。

是否可以在.gitconfig中指定多个[用户]定义?或者是否有其他方法覆盖特定目录的默认.gitconfig ?在我的情况下,我检查了~/worksrc/中的所有工作代码-是否有一种方法只为该目录(及其子目录)指定.gitconfig ?


当前回答

可以有多种方法来做到这一点,但我通过下面的shell函数来实现这一点。

function gitprofile() { if [[ $1 == "private" ]]; then if grep -q "tom@workmail.com" "/Users/tomtaylor/.gitconfig"; then echo "Found in gitconfig. No action required." else echo "Found in gitconfig1" cp /Users/tomtaylor/.gitconfig /Users/tomtaylor/.gitconfig2 mv /Users/tomtaylor/.gitconfig1 /Users/tomtaylor/.gitconfig mv /Users/tomtaylor/.gitconfig2 /Users/tomtaylor/.gitconfig1 fi elif [[ $1 == "public" ]]; then if grep -q "tom@gmail.com" "/Users/tomtaylor/.gitconfig"; then echo "Found in gitconfig. No action required." else echo "Found in gitconfig1" cp /Users/tomtaylor/.gitconfig /Users/tomtaylor/.gitconfig2 mv /Users/tomtaylor/.gitconfig1 /Users/tomtaylor/.gitconfig mv /Users/tomtaylor/.gitconfig2 /Users/tomtaylor/.gitconfig1 fi }

比如,

gitprofile "public" ->这将切换到配置tom@gmail.com和

gitprofile "private" ->这会切换到tom@workmail.com。

将此函数添加到~/。Bash_profile或~/。ZSHRC基于您当前的终端偏好。重新启动终端或像这样编译脚本

. ~ / . bash_profile

使函数有效。希望能有所帮助!

其他回答

这个答案部分是受到@Saucier的帖子的启发,但我正在寻找一种自动设置user.name和user的方法。基于远程的,每回购一次的电子邮件,这比他开发的git-passport包要轻一些。也h/t @John为useConfigOnly设置。以下是我的解决方案:

.gitconfig变化:

[github]
    name = <github username>
    email = <github email>
[gitlab]
    name = <gitlab username>
    email = <gitlab email>
[init]
    templatedir = ~/.git-templates
[user]
    useConfigOnly = true

Post-checkout钩子应该保存到以下路径:~/.git-templates/hooks/ Post-checkout:

#!/usr/bin/env bash

# make regex matching below case insensitive
shopt -s nocasematch

# values in the services array should have a corresponding section in
# .gitconfig where the 'name' and 'email' for that service are specified
remote_url="$( git config --get --local remote.origin.url )"
services=(
    'github'
    'gitlab'
)

set_local_user_config() {
    local service="${1}"
    local config="${2}"
    local service_config="$( git config --get ${service}.${config} )"
    local local_config="$( git config --get --local user.${config} )"

    if [[ "${local_config}" != "${service_config}" ]]; then
        git config --local "user.${config}" "${service_config}"
        echo "repo 'user.${config}' has been set to '${service_config}'"
    fi
}

# if remote_url doesn't contain the any of the values in the services
# array the user name and email will remain unset and the
# user.useConfigOnly = true setting in .gitconfig will prompt for those
# credentials and prevent commits until they are defined
for s in "${services[@]}"; do
    if [[ "${remote_url}" =~ "${s}" ]]; then
        set_local_user_config "${s}" 'name'
        set_local_user_config "${s}" 'email'
        break
    fi
done

我对github和gitlab使用不同的凭据,但上面代码中的那些引用可以用你使用的任何服务替换或增强。为了让签出后钩子自动在本地为签出后的回购设置用户名和电子邮件,确保服务名称出现在远程url中,将其添加到签出后脚本中的services数组中,并在.gitconfig中为它创建一个包含该服务的用户名和电子邮件的部分。

如果远程url中没有出现任何服务名称,或者repo没有远程用户名和电子邮件将不会在本地设置。在这些情况下,用户。useConfigOnly设置将在发挥,将不允许您提交,直到用户名和电子邮件设置为回购级别,并将提示用户配置该信息。

您可以将单个回购配置为使用覆盖全局配置的特定用户/电子邮件地址。从回购的根目录运行

git config user.name "Your Name Here"
git config user.email your@email.com

而缺省用户/电子邮件是在~/.gitconfig中配置的

git config --global user.name "Your Name Here"
git config --global user.email your@email.com

只需将此添加到~/。Bash_profile在github.com的默认键之间切换

# Git SSH keys swap
alias work_git="ssh-add -D  && ssh-add -K ~/.ssh/id_rsa_work"
alias personal_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa"

虽然大多数问题在某种程度上回答了OP,但我必须自己完成这个过程,甚至不用谷歌,我就能找到最快最简单的解决方案。以下是简单的步骤:

从其他回购中复制现有的.gitconf 粘贴到您新添加的回购 修改.gitconfig文件中的值,如名称、电子邮件和用户名 (用户) 名字=约翰 邮箱= john@email.net 用户名= john133 将filename添加到.gitignore列表中,以确保不会将.gitconfig文件提交到工作回购中

通过Git 2.13中的条件包含,现在可以在一台机器上用很少的工作同时拥有多个用户/电子邮件。

用户。Gitconfig有我的个人姓名和电子邮件。work-user。Gitconfig有我的工作名称和电子邮件。两个文件都位于~ path。

所以我的个人姓名/电子邮件默认适用。对于c:/work/ dir,应用的是我的工作名称/电子邮件。c:/work/github/ dir,应用我的个人姓名/邮箱。这在应用最后一个设置时起作用。

# ~/.gitconfig
[include]
    path = user.gitconfig
[includeIf "gitdir/i:c:/work/"]
    path = work-user.gitconfig
[includeIf "gitdir/i:c:/work/github/"]
    path = user.gitconfig

Gitdir区分大小写,Gitdir /i不区分大小写。

"gitdir/i:github/"将对路径中包含github的任何目录应用条件包含。