What are the type of git hooks?
Git supports several types of hooks, which are categorized as either client-side hooks or server-side hooks. Each type of hook corresponds to a specific stage in the Git workflow and allows you to execute custom scripts or actions at those stages. Here are the types of Git hooks:
1. Client-Side Hooks:
- pre-commit: Executed before a commit is created. It allows you to perform pre-commit checks, such as code linting, running tests, or checking for style violations. If the pre-commit hook fails, the commit process is aborted.
- prepare-commit-msg: Executed after the pre-commit hook but before the commit message editor is opened. It allows you to modify the commit message automatically or prepopulate it with certain information.
- commit-msg: Executed after the commit message editor is closed and the commit message is finalized. It enables you to enforce commit message guidelines, such as requiring specific keywords or formatting.
- post-commit: Executed after a commit is created. This hook is useful for performing post-commit actions, such as sending notifications or triggering a build.
- pre-rebase: Executed before a branch is rebased onto another branch. It allows you to run checks or prevent certain rebases.
- pre-push: Executed before a push to a remote repository. It can be used to prevent specific pushes, run additional checks, or enforce certain rules before code is sent to the remote repository.
2. Server-Side Hooks:
- pre-receive: Executed on the remote repository when receiving a push. It allows you to inspect the incoming changes and possibly reject the push based on custom criteria.
- update: Executed on the remote repository during push, after the pre-receive hook. It enables you to control which references (branches or tags) can be updated during the push.
- post-receive: Executed on the remote repository after the push is completed. It is commonly used for deployment tasks or to trigger actions on the server after receiving changes.
- pre-auto-gc: Executed before the server automatically runs the garbage collection process. It allows you to perform additional actions or checks before Git cleans up unused objects.
- post-rewrite: Executed after Git rewrites commits, such as during an interactive rebase. It allows you to react to changes in the commit history and perform additional actions.
To use Git hooks, you need to create executable scripts with the corresponding hook names in the
.git/hooks/ directory of your Git repository. These scripts will be automatically executed by Git when the corresponding events occur. By leveraging Git hooks, you can automate processes, enforce code quality, and ensure that your team follows best practices throughout the development workflow.