How do I make git block commits if user email isn’t set?

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“:

Leave a Comment