我可以配置git,因此它不会猜测user.email配置设置吗?

时间:2013-11-06 20:15:14

标签: git

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

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

有没有办法将git配置为出错而不是在我尝试提交时没有配置本地或全局user.email值?

5 个答案:

答案 0 :(得分:8)

现在有一个配置选项。

  

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

答案 1 :(得分: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 templates确保挂钩存在于将来的存储库中,方法是将挂钩放在模板文件夹中:

-> 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 templates文件夹已更新,则运行git init以创建新的挂钩文件。

答案 2 :(得分: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文件中使用init.templatedir配置值在系统范围内激活,我在另一个Stack Overflow post中看到了这一点。我将模板目录on github作为我的git-tools集合的一部分。

此外,我意识到我还可以在我的shell提示逻辑中执行检查,该逻辑已经向提示添加了git状态,当我进入没有{{的沙箱时打印一个大而肥胖的警告1}}已配置值。

在我的user.email

.bashrc

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

enter image description here

答案 3 :(得分: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).

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

答案 4 :(得分:1)

它可能非常 hokey并且可能在边缘情况下中断,但您可以编写一个系统范围的post-commit挂钩,查看新提交的作者并撤消它(通过git reset)如果它似乎是假的。

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