.git/hooks
Table of Contents
- Git Hooks
- Git-managed(shared) hook scripts: copy or symlink them to
.git/hooks
- In a git hook, is the working directory guaranteed to be within the git repository?
Git Hooks tutorial
applypatch-msg.sample
commit-msg.sample
fsmonitor-watchman.sample
post-update.sample
pre-applypatch.sample
pre-commit.sample
pre-push.sample
pre-rebase.sample
pre-receive.sample
prepare-commit-msg.sample
update.sample
You can modify these files and remove .sample
extension to activate hooks.
There are two kinds of hooks:
- Local Hooks
pre-commit
prepare-commit-msg
commit-msg
post-commit
post-checkout
pre-rebase
- Server-side Hooks
pre-receive
update
post-receive
Git-managed(shared) hook scripts: copy or symlink them to .git/hooks
discussion
Managing hooks under the repository is good to share the same scripts between developers. As we can't manage files under .git
we should decided to copy or symlink scripts into .git/hooks
.
Make sure the scripts are executable.
Copy if your scripts are independent. In this way, the hooks will work consistently regardless of the current branch.
cp scripts/pre-push .git/hooks/
Symlink if your scripts are constantly changing. In this way, developers don't need to copy them to
.git/hooks
every time the scripts change.ln -s ../../scripts/foo.sh .git/hooks/pre-commit
In a git hook, is the working directory guaranteed to be within the git repository? discussion
Before Git invokes a hook, it changes its working directory to either the root of the working tree in a non-bare repository, or to the
$GIT_DIR
in a bare repository.