Reputation: 1263
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 <[email protected]>
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.
Upvotes: 11
Views: 957
Reputation: 513
Execute the following command to prevent :
git config --global user.useConfigOnly true
or even execute:
git config --system user.useConfigOnly true
By default, git config user.useConfigOnly
is set to false
, so git will try to autoconfigure user.name
and user.email
while committing if unset.
But on some platforms (e.g, Ubuntu), a really wired behavior is that git won't autoconfigure user.name
even you leave user.name
empty, and "identity unknown" error will occur if you try to commit, so most people won't meet this autoconfigure thing at all, until you have a repo set user.name
but not user.email
(in short, git will only autoconfigure user.email
).
I assume there are many people like me only want to configure user.name
but not user.email
globally to prevent to commit with the wrong email, and definitely don't want to have that autoconfigured "email" thing, if so, just run the above command.
Upvotes: 0
Reputation: 60003
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.
Upvotes: 8
Reputation: 1263
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 [email protected]' 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 with git init
.
Upvotes: 1