Git Hook to Check user.email Before Every Commit
To stop a commit with the wrong email, add a pre-commit hook that compares git config user.email to the address you expect for that repo and exits non-zero on a mismatch. Git then refuses the commit before it exists, which beats rewriting history afterwards.
Why a hook, and not just a better config?
Config tells git who you should be. A hook checks that it actually worked. includeIf fails silently the moment you clone outside a bound folder, and a global user.email quietly fills the gap. The hook turns that silent failure into a loud one.
The simplest pre-commit email check
Create .git/hooks/pre-commit in the repo and make it executable:
#!/bin/sh
expected="dev@acme.com"
actual="$(git config user.email)"
if [ "$actual" != "$expected" ]; then
echo "Wrong email: got '$actual', expected '$expected'." >&2
echo "Fix: git config user.email $expected" >&2
exit 1
fi
chmod +x .git/hooks/pre-commit
Try a commit with the wrong identity and git stops with your message. A non-zero exit from pre-commit aborts the commit.
How do I share the hook across repos?
.git/hooks is not versioned and is per clone. Two common fixes:
- Per repo, committed: put the script in
.githooks/pre-commitand rungit config core.hooksPath .githooks. Teammates must run that command once after cloning. - Per machine: set a global hooks directory with
git config --global core.hooksPath ~/.config/git/hooks. Beware: this replaces each repo’s own.git/hooks, so tools like husky stop firing unless your hook chains to them.
Can the hook check the author, not just the config?
Partly. git config user.email reads configuration, but a commit can override it with --author or the GIT_AUTHOR_EMAIL environment variable. To see the identity git will really use, ask git itself:
git var GIT_AUTHOR_IDENT
Note that --author is applied after pre-commit runs, so a pre-commit check can miss it. If that matters, do the same check in a commit-msg hook, at the cost of having already typed the message.
Which email should the hook expect?
Hardcoding one address per repo does not scale. A better rule is to compare against a domain or a lookup by remote:
remote="$(git remote get-url origin 2>/dev/null)"
case "$remote" in
*acme-inc/*) expected="dev@acme.com" ;;
*) expected="me@personal.dev" ;;
esac
Now the expectation follows the organization in the remote URL, not the path on your disk.
What a hook cannot do
--no-verifyskips it. Hooks are a seatbelt, not a lock. For a hard guarantee you need server-side rules, such as a GitHub push ruleset restricting commit email patterns.- Email is one of four layers. The wrong SSH key, the wrong
ghsession and a missing signature still slip through. See why gh auth switch misses half the problem and the unverified commit badge. - Writing and maintaining the script is on you. Every new account means another branch in the
case.
Skip the scripting: let a guard check all four layers
Hats ships this as a built-in guard. Bind a folder to a profile, and a hook blocks the commit when the repo’s expected hat does not match the active identity, then prints the one command that fixes it (hats use acme). It chains your existing hooks, so husky and commitlint keep working. Install it with brew install --cask xorbeta/hats/hats.
FAQ
Does a pre-commit hook run on git commit —amend?
Yes. pre-commit runs on amend too, which is handy when you want to fix an author mistake before pushing. To change an existing commit’s author, use git commit --amend --reset-author.
Can I enforce this for the whole team?
Commit the hook to .githooks/ and document the core.hooksPath setting in your README. Pair it with a server-side rule, because local hooks can always be bypassed.
Does the hook see includeIf values?
Yes. Git resolves includeIf when it reads config, so git config user.email inside the hook returns the value for the current repo, which is exactly what you want to verify.
What about pushes that already contain the wrong email?
The hook only protects future commits. For existing ones, see the permission denied guide and rewrite the author before anyone pulls.
The short version
Check git config user.email in pre-commit, share it with core.hooksPath, and remember --no-verify exists. A hook turns a silent identity leak into a refusal you can fix in ten seconds.