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

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

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


当前回答

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

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

其他回答

有点像Rob W的答案,但允许不同的ssh密钥,并适用于旧版本的git(没有例如核心)。sshCommand配置)。

我创建了~/bin/git_poweruser文件,具有可执行权限,并在PATH中:

#!/bin/bash

TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT

cat > $TMPDIR/ssh << 'EOF'
#!/bin/bash
ssh -i $HOME/.ssh/poweruserprivatekey $@
EOF

chmod +x $TMPDIR/ssh
export GIT_SSH=$TMPDIR/ssh

git -c user.name="Power User name" -c user.email="power@user.email" $@

每当我想以“超级用户”的身份提交或推送一些东西时,我使用git_poweruser而不是git。它可以在任何目录下工作,并且不需要更改.gitconfig或.ssh/config,至少在我的目录中不需要更改。

从Orr Sella的博客文章中得到一些灵感后,我写了一个预提交钩子(驻留在~/.git/templates/hooks中),它会根据本地存储库的。/.git/config中的信息设置特定的用户名和电子邮件地址:

你必须把模板目录的路径放到~/.gitconfig中:

[init]
    templatedir = ~/.git/templates

然后,每个git init或git克隆都将获得该钩子,并在下一次git提交期间应用用户数据。如果你想将钩子应用到已经存在的repo上,那么只需在repo中运行git init来重新初始化它。

这是我想出的钩子(它仍然需要一些抛光-欢迎提出建议)。 另存为

~/.git/templates/hooks/pre_commit

or

~/.git/templates/hooks/post-checkout

并确保它是可执行的:chmod +x ./post-checkout || chmod +x ./pre_commit

#!/usr/bin/env bash

# -------- USER CONFIG
# Patterns to match a repo's "remote.origin.url" - beginning portion of the hostname
git_remotes[0]="Github"
git_remotes[1]="Gitlab"

# Adjust names and e-mail addresses
local_id_0[0]="my_name_0"
local_id_0[1]="my_email_0"

local_id_1[0]="my_name_1"
local_id_1[1]="my_email_1"

local_fallback_id[0]="${local_id_0[0]}"
local_fallback_id[1]="${local_id_0[1]}"


# -------- FUNCTIONS
setIdentity()
{
    local current_id local_id

    current_id[0]="$(git config --get --local user.name)"
    current_id[1]="$(git config --get --local user.email)"

    local_id=("$@")

    if [[ "${current_id[0]}" == "${local_id[0]}" &&
          "${current_id[1]}" == "${local_id[1]}" ]]; then
        printf " Local identity is:\n"
        printf "»  User: %s\n»  Mail: %s\n\n" "${current_id[@]}"
    else
        printf "»  User: %s\n»  Mail: %s\n\n" "${local_id[@]}"
        git config --local user.name "${local_id[0]}"
        git config --local user.email "${local_id[1]}"
    fi

    return 0
}

# -------- IMPLEMENTATION
current_remote_url="$(git config --get --local remote.origin.url)"

if [[ "$current_remote_url" ]]; then

    for service in "${git_remotes[@]}"; do

        # Disable case sensitivity for regex matching
        shopt -s nocasematch

        if [[ "$current_remote_url" =~ $service ]]; then
            case "$service" in

                "${git_remotes[0]}" )
                    printf "\n»» An Intermission\n»  %s repository found." "${git_remotes[0]}"
                    setIdentity "${local_id_0[@]}"
                    exit 0
                    ;;

                "${git_remotes[1]}" )
                    printf "\n»» An Intermission\n»  %s repository found." "${git_remotes[1]}"
                    setIdentity "${local_id_1[@]}"
                    exit 0
                    ;;

                * )
                    printf "\n»  pre-commit hook: unknown error\n» Quitting.\n"
                    exit 1
                    ;;

            esac
        fi
    done
else
    printf "\n»» An Intermission\n»  No remote repository set. Using local fallback identity:\n"
    printf "»  User: %s\n»  Mail: %s\n\n" "${local_fallback_id[@]}"

    # Get the user's attention for a second
    sleep 1

    git config --local user.name "${local_fallback_id[0]}"
    git config --local user.email "${local_fallback_id[1]}"
fi

exit 0

编辑:

因此,我将钩子重写为Python中的钩子和命令。此外,还可以将脚本作为Git命令(Git passport)调用。此外,还可以在configfile (~/.gitpassport)中定义任意数量的id,这些id可以在提示符中选择。你可以在github.com上找到这个项目:Git -passport -一个用Python编写的Git命令和钩子,用于管理多个Git帐户/用户身份。

有一个简单的解决方案似乎可以很好地避免错误。

只需从~/中删除[user]部分。如果不为每个存储库设置user.name,就不能进行任何提交。

在你的~/。Bashrc,为用户和电子邮件添加一些简单的别名:

alias ggmail='git config user.name "My Name";git config user.email me@gmail.com'
alias gwork='git config user.name "My Name";git config user.email me@work.job'

如果你不想有一个默认的电子邮件地址(电子邮件地址链接到一个github用户),你可以配置你想要被问到。如何做到这一点取决于你使用的git版本,见下文。

(预期的)缺点是您必须为每个存储库配置一次电子邮件地址(和您的姓名)。所以,你不能忘记去做。

版本< 2.7.0

[user]
    name = Your name
    email = "(none)"

在全局配置中~/。Dan Aloni在Orr Sella的博客文章中评论了gitconfig。当尝试在存储库中进行第一次提交时,git失败了,并给出了漂亮的消息:

*** 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 '(none)')

当本地设置电子邮件地址时,该名称取自全局配置(消息不是完全准确)。

2.7.0≤版本< 2.8.0

版本< 2.7.0中的行为不是预期的,并在2.7.0中修复。您仍然可以使用Orr Sella的博客文章中描述的预提交钩子。此解决方案也适用于其他版本,但其他解决方案不适用于此版本。

版本≥2.8.0

Dan Aloni添加了一个选项来实现这种行为(参见发布说明)。用它:

[user]
    useConfigOnly = true

为了让它工作,你可以不给一个名字或电子邮件地址在全局配置。然后,在第一次提交时,您会得到一条错误消息

fatal: user.useConfigOnly set but no name given

因此,该消息不是很有指导意义,但由于您显式地设置了该选项,您应该知道该做什么。与< 2.7.0版本的解决方案相反,您总是必须手动设置名称和电子邮件

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

它只在当前存储库中设置您的姓名和电子邮件。

或者您可以在本地的.git/config文件中添加以下信息

[user]  
    name = Your Name
    email = your.email@gmail.com