如果未设置用户电子邮件,我该如何做git块提交? [英] How do I make git block commits if user email isn't set?

查看:312
本文介绍了如果未设置用户电子邮件,我该如何做git块提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用于工作项目和开源项目的单独电子邮件地址,我想确保每种类型的项目都使用正确的电子邮件地址。



很明显,解决方案是适当地设置存储库特定的配置,不幸的是我一直忘记设置它,直到我提交了一些提交,因此我正在使用全局user.email配置,如果这符合我的'如果user.email没有设置任何地方git只是crams本地用户名和主机在一起,使一个(不是)对于台式机来说非常棒);我想它只是阻止提交。我不认为钩子会起作用,因为我希望这种情况发生在新的&克隆回购 - 如果我手动将钩子复制到回购,我应该可以设置配置。

解决方案

新更新(2016年2月,对于git 2.8)

2.8(2016年3月)将增加一个新的解决方案:

请参阅提交4d5c295 (2016年2月6日) .com / da-xrel =noreferrer> Dan Aloni( da-x )。

帮助:< a href =https://github.com/sunshineco =noreferrer> Eric Sunshine( sunshineco )。

请参阅提交59f9295 (2016年2月4日) .com / peffrel =noreferrer> Jeff King( peff )。
帮助: Eric Sunshine( sunshineco

(由 Junio C Hamano - gitster - 合并提交c37f9a1 ,2016年2月17日)


ident :add user.useConfigOnly boolean for when ident 不应该被猜出



它曾经是:




  git config --global user.email(none)




是人们强制自己在$ b中设置 user.email 的一种可行方式$ b每个存储库。

这对于拥有多个电子邮件地址的人有帮助,针对不同克隆的不同电子邮件地址,因为它禁止git创建提交,除非用户。电子邮件 config在每个回购配置中设置为正确的电子邮件地址。



更改, 19ce497 ident :为git 2.6.5,2016年1月保留一个伪造
的default_email,2015-12-10标志),但是,声明明确配置了 user.email 不是伪造的,不管它的价值是什么,所以这个破解不再有效。

通过添加一个新配置来提供相同的功能变量 user.useConfigOnly ;当设置这个变量时,
用户必须明确地设置user.email配置


2016 and git 2.8,do:

  git config --global user.useConfigOnly true 

您的任何新回购将仅查找 user.email 在其本地 .git / config 文件中。

如果没有 user.email 在本地git配置中找到。






请注意,它会在git 2.9 2016年6月):

请参阅提交d3c06c1 提交734c778 (2016年3月30日)作者: Marios Titas(``)
(由 Junio C Hamano - gitster - commit e7e6826 ,2016年4月29日)


< h2> ident :在自动检测名称/电子邮件之前检查 useConfigOnly

如果设置 user.useConfigOnly ,尝试自动检测名称和/或电子邮件是没有意义的。

自动检测甚至可能导致伪造的名称并触发错误消息。



ident :give 请告诉我在 useConfigOnly 错误



的消息中使用较少描述性的错误消息来阻止用户禁用 user.useConfigOnly 配置变量来解决这个错误条件。我们希望鼓励他们设置user.name或user.email。


所以不是:

  user.useConfigOnly设置但没有给定名称
user.useConfigOnly设置但没有给定的邮件

您会看到:

  -detection被禁用
没有给出电子邮件并且自动检测被禁用






原始答案(2012年9月)

有点像使用预先提交钩子停止特定作者的git提交,您可以定义默认 pre-commit钩子检查是否:

  git config --local user.email 

不为空。

如果为空:退出1



为确保您使用的是默认钩子f或者您创建的每个回购,请参阅更改默认git挂钩

这是一种类似于分享您的内容git hooks


I have separate email addresses that I use for work projects vs open source projects, and I want to ensure that I use the correct one for each type of project.

Obviously the solution is to set the repository specific configuration appropriately, unfortunately I keep forgetting to set it until I'm a few commits in and so am using the global user.email config, which is fine if that matches up with what I'm working on, but not so fine otherwise.

If the user.email isn't set anywhere git just crams the local username and host together to make one up (not so awesome for a desktop machine); I'd like it to just block the commit. I don't think hooks will work, since I want this to happen for new & cloned repos - if I'm manually copying a hook into a repo, I should probably set the config instead.

解决方案

New Update (February 2016, for git 2.8)

git 2.8 (March 2016) will add a new solution:

See commit 4d5c295 (06 Feb 2016) by Dan Aloni (da-x).
Helped-by: Eric Sunshine (sunshineco).
See commit 59f9295 (04 Feb 2016) by Jeff King (peff).
Helped-by: Eric Sunshine (sunshineco).
(Merged by Junio C Hamano -- gitster -- in commit c37f9a1, 17 Feb 2016)

ident: add user.useConfigOnly boolean for when ident shouldn't be guessed

It used to be that:

git config --global user.email "(none)"

was a viable way for people to force themselves to set user.email in each repository.
This was helpful for people with more than one email address, targeting different email addresses for different clones, as it barred git from creating a commit unless the user.email config was set in the per-repo config to the correct email address.

A recent change, 19ce497 (ident: keep a flag for bogus default_email, 2015-12-10 for git 2.6.5, Jan. 2016), however, declared that an explicitly configured user.email is not bogus, no matter what its value is, so this hack no longer works.

Provide the same functionality by adding a new configuration variable user.useConfigOnly; when this variable is set, the user must explicitly set user.email configuration.

So starting March 2016 and git 2.8, do:

git config --global user.useConfigOnly true

Any of your new repo will look for user.email only in their local .git/config file.
And the commit will not proceed if no user.email is found in the local git config.


Note that it will be enhanced some more in git 2.9 (June 2016):

See commit d3c06c1, commit 734c778 (30 Mar 2016) by Marios Titas (``).
(Merged by Junio C Hamano -- gitster -- in commit e7e6826, 29 Apr 2016)

ident: check for useConfigOnly before auto-detection of name/email

If user.useConfigOnly is set, it does not make sense to try to auto-detect the name and/or the email.
The auto-detection may even result in a bogus name and trigger an error message.

ident: give "please tell me" message upon useConfigOnly error

use a less descriptive error message to discourage users from disabling user.useConfigOnly configuration variable to work around this error condition. We want to encourage them to set user.name or user.email instead.

So instead of:

user.useConfigOnly set but no name given
user.useConfigOnly set but no mail given

You will see:

no name was given and auto-detection is disabled
no email was given and auto-detection is disabled


Original answer (Sept. 2012)

A bit like in "Stop a git commit by a specific author using pre-commit hook", you could define a default pre-commit hook which check if:

 git config --local user.email

is empty or not.
If it is empty: exit 1

To make sure you are using that default hook for every repo you are creating, see "change default git hooks".
It is a similar approach that the one described in "Share your git hooks":

这篇关于如果未设置用户电子邮件,我该如何做git块提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆