Overview
Teaching: 5 min
Exercises: 0 minQuestionsObjectives
- How can we automate tasks depending on events in the Git repository?
- Learn how to couple scripts to Git repository events.
Sometimes you would like Git events (commits, pushes, etc.) to trigger scripts which take care of some tasks.
Hooks are scripts that are executed before/after certain events.
They can be used to enforce nearly any kind of policy for your project.
There are client-side and server-side hooks.
You can find and edit them here:
$ ls -l .git/hooks/
pre-commit
: before commit message editor (example: make sure tests pass)prepare-commit-msg
: before commit message editor (example: modify default messages)commit-msg
: after commit message editor (example: validate commit message pattern)post-commit
: after commit process (example: notification)pre-rebase
: before rebase anything (example: disallow rebasing published commits)post-rewrite
: run by commands that rewrite commitspost-checkout
: after successful git checkout
(example: generating documentation)post-merge
: after successful mergepre-push
: runs during git push
before any objects have been transferredpre-auto-gc
: invoked just before the garbage collection takes placeSee also pre-commit, a framework for managing and maintaining multi-language pre-commit hooks.
Example for a pre-commit
hook which checks whether a Python code is PEP 8-compliant
using pycodestyle:
#!/usr/bin/env bash
# ignore errors due to too long lines
pycodestyle --ignore=E501 myproject/
You can typically edit them through a web interface on GitHub/GitLab.
pre-receive
: before accepting any referencesupdate
: like pre-receive
but runs once per pushed branchpost-receive
: after entire process is completed