Nach den Tatsachen
Wo Commit-Zeit-Hooks Commits formen, reagieren die Hooks auf dieser Seite auf sie. post-commit lauft lokal nach jedem Commit, pre-push lauft vor Datentransfer zum Remote, und post-receive lauft auf dem Server nachdem ein Push gelandet ist.
post-commit
#!/usr/bin/env bash
ctags -R --languages=python -f .tags & disown
notify-send "Committed: $(git log -1 --format=%s)"
pre-push
#!/usr/bin/env bash
remote="$1"
url="$2"
while read local_ref local_sha remote_ref remote_sha; do
if [ "$remote_ref" = "refs/heads/main" ]; then
if ! cargo test --quiet; then
echo "Tests fail; refusing to push to main."
exit 1
fi
fi
done
post-receive (server-seitig)
#!/usr/bin/env bash
while read oldrev newrev refname; do
branch=${refname#refs/heads/}
if [ "$branch" = "main" ]; then
GIT_WORK_TREE=/var/www/site git checkout -f main
curl -X POST https://chat.example/notify \
-d "Deployed $newrev to production"
fi
done
Andere Server-Hooks
pre-receive lauft einmal pro Push und kann den gesamten Push ablehnen. update lauft einmal pro Ref.
Hook-Verkettung
#!/usr/bin/env bash
for hook in .githooks/post-commit.d/*; do
[ -x "$hook" ] && "$hook" "$@" || exit $?
done
Haufige Fehler
post-commit kann einen Commit nicht verhindern; keine Validierung dort einbauen.
Debugging
exec >> /tmp/git-hook.log 2>&1
set -x