问题
Is there a way to globally configure git
to not automatically generate user's email, if none is set and abort the commit instead?
$ git commit -m "test"
[master (root-commit) 71c3e2e] test
Committer: unknown <my.user@my.very.own.hostname>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
This can cause serious privacy leaks if the committer is not careful enough to check git
warnings.
回答1:
Set git config --global user.useConfigOnly true
user.useConfigOnly
Instruct Git to avoid trying to guess defaults for user.email and user.name, and instead retrieve the values only from the configuration. For example, if you have multiple email addresses and would like to use a different one for each repository, then with this configuration option set to true in the global config along with a name, Git will prompt you to set up an email before making new commits in a newly cloned repository. Defaults to false.
回答2:
This can be done with git
pre-commit hook, following the suggestion by @cupcake.
Create file named
pre-commit
like so:#!/bin/sh git config user.name >/dev/null 2>&1 if [[ $? != 0 ]]; then echo "Error: user.name is undefined. Use 'git config user.name \"Your Name\"' to set it." exit 1 fi git config user.email >/dev/null 2>&1 if [[ $? != 0 ]]; then echo "Error: user.email is undefined. Use 'git config user.email you@example.com' to set it." exit 1 fi
If necessary, make it executable with
chmod +x pre-commit
Throw this file to global
git
hook templates:On *nix systems this is located at
/usr/share/git-core/templates/hooks
On Windows this can be typically found in
%ProgramFiles%\Git\share\git-core\templates\hooks
Re-initialize your existing
git
repos withgit init
.
来源:https://stackoverflow.com/questions/23457116/prevent-git-from-automatically-generating-user-email-if-missing