utility

Git pre-commit hook

Executable local hook invoked before committing; nonzero aborts the commit. Can be bypassed; CI must independently validate source. Fixture uses default hooks path in disposable repo.

Formal Syntax & Structure

bash syntaxbash
.git/hooks/pre-commit

Return Value

exit status

Executable local hook invoked before committing; nonzero aborts the commit. Can be bypassed; CI must independently validate source. Fixture uses default hooks path in disposable repo.

Working Example

Example Snippetbash
export GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null git init -q --template= repo || exit 1cd repo || exit 1mkdir -p .git/hooks || exit 1printf '#!/bin/sh\nexit 3\n' > .git/hooks/pre-commitchmod +x .git/hooks/pre-commitprintf 'fixture\n' > note.txtgit add note.txt || exit 1if git -c user.name=Lab -c user.email=lab@example.invalid commit -qm fixture 2>diagnostic.txt; then    exit 1fiif git rev-parse --verify HEAD >/dev/null 2>&1; then exit 1; fiprintf 'commit blocked\n'
Bash Reference