Git hooks are Git's in-built method of triggering scripts based on actions. This repo's purpose is to provide an overview of the different types of git hooks and example use cases.
- Implementation
- Different Types of Hooks
- Committing Hooks
- Manually tracking and installing hooks
- Using a Git Hook Manager (Husky)
- Resources
Hooks are stored in .git/hooks folder. By default .git/hooks will be populated with example hooks, if you want to enable them:
- Remove the ".sample" suffix from the file name
- Make the hook executable using:
chmod +x .git/hooks/hook-name - Run
npm ito install them
When choosing a Git hook, consider:
- How frequently the hook will be triggered
- How long the hook's operations will take to run
- The impact on developer workflow
- Whether the operation needs to run on every commit or can wait until push
Client side hooks are for operations that run on your local device (e.g. commit, rebase). Server side hooks are for network operations that interact with the remote Git server (e.g. push).
| Name | Type | Trigger |
|---|---|---|
| pre-commit | client-side | git commit |
| commit-msg | client-side | git commit |
| pre-push | client-side | git push |
| pre-rebase | client-side | git rebase |
| pre-merge-commit | server-side | before merge |
| post-merge | server-side | after merge |
| pre-receive | server-side | before push to remote |
| update | server-side | after push to remote |
| post-receive | server-side | after push to remote |
| post-update | server-side | after push to remote |
| pre-applypatch | client-side | git am |
| applypatch-msg | client-side | git am |
| post-applypatch | client-side | git am |
| pre-commit | client-side | git commit |
| prepare-commit-msg | client-side | git commit |
| post-commit | client-side | git commit |
| pre-auto-gc | client-side | git gc |
| post-rewrite | client-side | git rebase/amend |
| pre-push | client-side | git push |
| pre-push | client-side | git push |
Analyses staged code. Best suited for quick checks that provide immediate feedback.
Example Use Cases:
- blocking certain code (e.g console.log)
- checking file size or lines of code exceeds limit
- formatting code
prevent-console-logs: Checks if any staged files contain "console.log", if so, cancels the commit and tells the user what file and line the log is on.
limit-line-number: Checks if any staged files contain more than a specified line number, if so, cancels the commit and tells the user what files exceed the limit
Can change the contents of a commit message. Ideal for maintaining commit message standards.
Example Use Cases:
- Adding ticket number to commit msg
- Enforcing commit message format - blocking vague messages (e.g. fix)
include-ticket-number: Add ticket numbers to commit messages if they are missing, prevents commits to branches without ticket numbers in their name. This means direct commits to master will be prevented.
[!NOTE] > pre-commit and commit-msg hooks can be bypassed with
git commit --no-verify
Looks at all commits since the last push. Best suited for time-consuming operations since pushing is less frequent than committing.
Example Use Cases:
- Running comprehensive test suites
- Checking for secrets or sensitive data
- Running full codebase linting
- Validating API documentation
- Preventing commits that start with "WIP"
These hooks are ideal for team-wide notifications and deployment preparations.
Example Use Cases:
- Notifying team via Slack/Teams
- Triggering CI/CD pipelines
- Updating documentation
- Running deployment checks
send-message-to-slack: Send messages to slack when certain actions happen (e.g. if a branch is merged). Pre-requisite set up a webhook for your Slack workspace. Update the example url in the script with your generated webhook url
Git hooks are ignored by default. If you want to commit them, you can do so manually or use a git hook manager:
- Create a
hooks/directory in your repository to store the hooks:mkdir hooks
- Add hooks to version control:
git add hooks/commit-msg git commit -m "Add commit-msg hook" - Create a setup-hooks.sh script to install the hooks:
#!/bin/bash cp hooks/* .git/hooks/ chmod +x .git/hooks/* npm i
- Make the script executable:
chmod +x setup-hooks.sh
- Run the script:
bash ./setup-hooks.sh[!NOTE] Optional: Add a script to your package.json, so you can run it with
npm run setupinstead"scripts": { "setup": "./setup-hooks.sh" }
For Node.js projects, you can use Husky to manage Git hooks more easily. Husky automatically sets up Git hooks when you install it and manages them through your package.json. To install and initialise husky in your project, run:
npm install husky --save-dev
npx husky installAfter installation, you can store hooks in the .husky/hooks directory. They will be installed the next time you run npm i. You can commit husky files directly with no need to copy them to the .git/hooks folder.
If your project is not based on Node.js, most runtime environments have Git hook managers tailored for them. For example, in Kotlin projects, you can use libraries like as Ktlint or plugins like Gradle's git-hooks plugin, which provide similar functionalities for managing hooks.



