On my system I don\'t have the user.email
git configuration value set at the global level, on purpose. Instead, I configure it individually in each sandbox. This is
The pre-commit solutions proposed here work well and I will accept one of them. I ended up using this:
if [ -z $(git config user.email) ]; then
echo "You need to set your user.email configuration value for this project"
echo "git config user.email foo@example.com"
exit 1
fi
It gets activated system-wide in my ~/.gitconfig
file with the init.templatedir
config value, which I saw mentioned in another Stack Overflow post. I put my template dir on github as part of my git-tools collection.
Additionally, I realized that I can also just perform a check in my shell prompt logic, which already adds git status to the prompt, to print a big, fat warning when I cd into a sandbox that does not have the user.email
value configured.
In my .bashrc
:
PS1='\u@\h:\W$(parse_git_branch) \$ '
parse_git_branch() {
local DIRTY STATUS EMAIL_NOT_CONFIGURED_WARNING
STATUS=$(git status 2>/dev/null)
[ $? -eq 128 -o $? -eq 127 ] && return
[ -z $(git config user.email) ] && EMAIL_NOT_CONFIGURED_WARNING=$' \x1b[31m(user.email not configured)\x1b[m'
[[ "$STATUS" == *'working directory clean'* ]] || DIRTY=' *'
echo "($(git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* //')$DIRTY)$EMAIL_NOT_CONFIGURED_WARNING"
}
This produces an easily visible warning:
It doesn't look like this is possible. From git-commit-tree(1)
:
While parent object ids are provided on the command line, author and committer
information is taken from the following environment variables, if set:
GIT_AUTHOR_NAME
GIT_AUTHOR_EMAIL
GIT_AUTHOR_DATE
GIT_COMMITTER_NAME
GIT_COMMITTER_EMAIL
GIT_COMMITTER_DATE
EMAIL
(nb "<", ">" and "\n"s are stripped)
In case (some of) these environment variables are not set, the information is
taken from the configuration items user.name and user.email, or, if not
present, system user name and the hostname used for outgoing mail (taken from
/etc/mailname and falling back to the fully qualified hostname when that file
does not exist).
Perhaps it's easier for you to configure environment variables for each sandbox?
It would be very hokey and possibly break in edge cases, but you could write a system-wide post-commit
hook that looks at the author of the new commit and undoes it (via git reset
) if it appears to be bogus.
Other than that, no, Git is happy to use whatever ridiculous email address it cobbles together as a default.
There is a config option for that now.
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.
So just set this to true like this:
git config --global user.useConfigOnly true
And next time when you try to make a commit without user.email and user.name explicitly set you will get an error.
$ git commit
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: no email was given and auto-detection is disabled
You can use a pre-commit hook to prompt you to setup your project-specfic email address. For example:
#!/bin/sh
email=`git config user.email`
if ["$email" = '']
then
echo "you need to set your email for this project"
exit 1
fi
This will cause committing without the appropriate config to fail:
$ git commit -va
you need to set your email for this project
$
You can use git templates to make sure the hook is present in future repositories by default, by placing the hook in the templates folder:
-> tree /usr/share/git-core/templates/
/usr/share/git-core/templates/
├── branches
├── description
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-rebase.sample
│ └── update.sample
└── info
└── exclude
The exact location of the templates folder may vary with OS/distribution.
For existing repositories - either create/copy the hook into place or if the git-core templates folder has been updated run git init
to create the new hook file.