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.
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)So starting March 2016 and git 2.8, do:
Any of your new repo will look for
user.emailonly in their local.git/configfile.And the commit will not proceed if no
user.emailis 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)So instead of:
You will see:
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:
is empty or not.
If it is empty:
exit 1To 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“: