11

在我的系统上,我没有user.email故意在全局级别设置 git 配置值。相反,我在每个沙箱中单独配置它。这是因为我需要为不同的项目使用不同的电子邮件地址。

不幸的是,我有时会在创建新沙箱时忘记配置值。在这些情况下,git 只是根据从环境中获得的信息“猜测”一个值。这会导致各种问题,例如提交在 github 上不属于我,并且我不会有太多运气@localhost来追溯这些提交的电子邮件地址归属于我。

当我在没有配置本地或全局值的情况下尝试提交时,有没有办法将 git 配置为出错而不是猜测user.email

4

5 回答 5

9

现在有一个配置选项。

user.useConfigOnly

指示 Git 避免尝试猜测 user.email 和 user.name 的默认值,而是仅从配置中检索值。

因此,只需将其设置为 true,如下所示:

git config --global user.useConfigOnly true

下次当您尝试在没有明确设置 user.email 和 user.name 的情况下进行提交时,您将收到错误消息。

$ git commit

*** 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: no email was given and auto-detection is disabled
于 2016-11-22T03:21:09.823 回答
3

使用预提交挂钩

您可以使用预提交挂钩来提示您设置项目特定的电子邮件地址。例如:

#!/bin/sh

email=`git config user.email`

if ["$email" = ''] 
then
    echo "you need to set your email for this project"
    exit 1
fi

这将导致在没有适当配置的情况下提交失败:

$ git commit -va
you need to set your email for this project
$

使用 git 的模板确保它始终存在

您可以使用git 模板来确保默认情况下该挂钩存在于未来的存储库中,方法是将挂钩放在模板文件夹中:

-> tree /usr/share/git-core/templates/
/usr/share/git-core/templates/
├── branches
├── description
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-rebase.sample
│   └── update.sample
└── info
    └── exclude

模板文件夹的确切位置可能因操作系统/发行版而异。

对于现有的存储库 - 创建/复制钩子到适当的位置,或者如果 git-core 模板文件夹已更新,则运行git init以创建新的钩子文件。

于 2013-11-06T21:37:46.827 回答
3

这里提出的预提交解决方案效果很好,我会接受其中之一。我最终使用了这个:

if [ -z $(git config user.email) ]; then
    echo "You need to set your user.email configuration value for this project"
    echo "git config user.email foo@example.com"
    exit 1
fi

~/.gitconfig它在我的文件中使用配置值在系统范围内激活,我在另一篇Stack Overflow 帖子init.templatedir中提到过。我将模板目录放在 github 上,作为我的git-tools集合的一部分。

此外,我意识到我也可以在我的 shell 提示逻辑中执行检查,它已经将 git status 添加到提示中,当我 cd 进入没有user.email配置值的沙箱时打印一个大而胖的警告。

在我的.bashrc

PS1='\u@\h:\W$(parse_git_branch) \$ '

parse_git_branch() {
    local DIRTY STATUS EMAIL_NOT_CONFIGURED_WARNING
    STATUS=$(git status 2>/dev/null)
    [ $? -eq 128 -o $? -eq 127 ] && return
    [ -z $(git config user.email) ] && EMAIL_NOT_CONFIGURED_WARNING=$' \x1b[31m(user.email not configured)\x1b[m'
    [[ "$STATUS" == *'working directory clean'* ]] || DIRTY=' *'
    echo "($(git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* //')$DIRTY)$EMAIL_NOT_CONFIGURED_WARNING"
}

这会产生一个容易看到的警告:

在此处输入图像描述

于 2013-11-06T23:43:48.997 回答
1

这将是非常狡猾的,并且可能在极端情况下中断,但是您可以编写一个系统范围的post-commit钩子来查看新提交的作者,git reset如果它看起来是虚假的,则将其撤消(通过)。

除此之外,不,Git 很乐意使用它拼凑的任何荒谬的电子邮件地址作为默认值。

于 2013-11-06T21:22:05.687 回答
1

看起来这是不可能的。来自git-commit-tree(1)

   While parent object ids are provided on the command line, author and committer
   information is taken from the following environment variables, if set:

       GIT_AUTHOR_NAME
       GIT_AUTHOR_EMAIL
       GIT_AUTHOR_DATE
       GIT_COMMITTER_NAME
       GIT_COMMITTER_EMAIL
       GIT_COMMITTER_DATE
       EMAIL

   (nb "<", ">" and "\n"s are stripped)

   In case (some of) these environment variables are not set, the information is
   taken from the configuration items user.name and user.email, or, if not
   present, system user name and the hostname used for outgoing mail (taken from
   /etc/mailname and falling back to the fully qualified hostname when that file
   does not exist).

也许为每个沙箱配置环境变量更容易?

于 2013-11-06T20:22:09.513 回答