Other pre-commit hooks#
The hooks managed by the pre-commit framework are not limited to being executed before commits; they can also be used for other Git hooks:
As of version 2.4.0, the framework can also execute post-commit hooks with:
$ pipenv run pre-commit install --hook-type post-commit pre-commit installed at .git/hooks/post-commit
post-commitdoes not work on files, all these hooks must set
- repo: local hooks: - id: post-commit-local name: post commit always_run: true stages: [post-commit] # …
As of Git 2.24, there is a pre-merge-commit hook that is triggered after a merge is successful but before the merge commit is created. You can use it with the pre-commit framework with:
$ pre-commit install --hook-type pre-merge-commit pre-commit installed at .git/hooks/pre-merge-commit
As of version 2.11.0, the framework can also execute scripts for the post-merge hook:
$ pipenv run pre-commit install --hook-type post-merge pre-commit installed at .git/hooks/post-merge
$PRE_COMMIT_IS_SQUASH_MERGEyou can find out if it was a squash merge.
To use the pre-push hook with the pre-commit framework, enter the following:
$ pre-commit install --hook-type pre-push pre-commit installed at .git/hooks/pre-push
The following environment variables are provided for this purpose:
The remote revision that was pushed to.
The local revision that was pushed to the remote revision.
The local revision that was pushed to the remote revision, for example
The URL of the remote repository that was pushed to, for example
The name of the remote branch that was pushed to, for example
The name of the local branch that was pushed to the remote branch, for example
commit-msg can be used with:
$ pre-commit install --hook-type commit-msg pre-commit installed at .git/hooks/commit-msg
commit-msghook can be configured with
stages: [commit-msg], passing the name of a file containing the current contents of the commit message that can be checked.
prepare-commit-msg can be used with pre-commit with:
$ pre-commit install --hook-type prepare-commit-msg pre-commit installed at .git/hooks/prepare-commit-msg
prepare-commit-msghook is configured with
stages: [prepare-commit-msg], passing the name of a file that contains the initial commit message, for example from
git commit -m "COMMIT-MESSAGE"to create a dynamic template from it that is displayed in the editor. Finally, the hook should check that no editor is started with
The post-checkout hook is called when
git switchis executed.
post-checkouthook can be used for example for
viewing differences from the previous
changing the metadata of the working directory.
In pre-commit it can be used with:
$ pre-commit install --hook-type post-checkout pre-commit installed at .git/hooks/post-checkout
post-checkout doesnot act on files,
always_runmust be set for all
post-checkoutscripts, for example:
- repo: local hooks: - id: post-checkout-local name: Post checkout always_run: true stages: [post-checkout] # …
There are three environment variables that correspond to the three arguments of
returns the reference of the previous
returns the reference of the new
HEAD, which may or may not have changed.
Flag=1if it was a branch checkout and
Flag=0if it was a file checkout.
post-rewrite is called when commits are rewritten, for example from
git commit --amendor from
$ pre-commit install --hook-type post-rewrite pre-commit installed at .git/hooks/post-rewrite
post-rewritedoes not affect files,
always_run: truemust be set.
Git tells the
post-rewritehook which command triggered the rewrite.
pre-commitoutputs this as