Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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.

like image 988
Cebjyre Avatar asked Sep 06 '12 03:09

Cebjyre


People also ask

Why are my commits linked to the wrong user?

GitHub uses the email address in the commit header to link the commit to a GitHub user. If your commits are being linked to another user, or not linked to a user at all, you may need to change your local Git configuration settings, add an email address to your account email settings, or do both.

How do I block emails on GitHub?

In the upper-right corner of any page, click . In the left sidebar, under the list of repositories, use the "Manage notifications" drop-down to click Subscriptions. Select the notifications you want to unsubscribe to. In the top right, click Unsubscribe.


2 Answers

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

like image 105
VonC Avatar answered Oct 06 '22 17:10

VonC


There is a hack/workaround I've just discovered, just set default global e-mail to (none), like this:

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

Little explanation: this workaround depends upon current email validation in the official git client source code so use it on your own risk.

like image 28
Sergey Stolyarov Avatar answered Oct 06 '22 16:10

Sergey Stolyarov