Git Hooks
What are Git Hooks?
Git hooks are scripts that Git automatically executes before or after certain events such as commits, pushes, merges, and checkouts. These hooks live in the .git/hooks directory of every Git repository and are an essential tool for automating tasks, enforcing rules, and improving development workflows.
Hooks are commonly used to:
- Run linters or formatters before a commit.
- Prevent accidental pushes to protected branches.
- Automatically update files or logs after a successful merge.
- Trigger notifications after important Git events.
Types of Git Hooks
Git provides two categories of hooks:
1. Client-side Hooks
These are triggered by operations such as commit, merge, rebase, checkout, and push. They run locally on the developer’s machine.
Some common client-side hooks include:
pre-commitprepare-commit-msgcommit-msgpost-commitpre-pushpost-mergepost-checkoutpre-rebase
2. Server-side Hooks
These are triggered during interactions with a remote repository, typically during git push. They run on the remote server.
Examples include:
pre-receiveupdatepost-receivepost-update
This blog focuses on client-side hooks which are most commonly used in daily development.
Client-side Git Hooks with Workflow
Below is a visual representation of when each Git hook is triggered in common Git workflows like commit, push, checkout, merge, and rebase.
Common Use Cases for Client-side Hooks
| Hook Name | When It Runs | Typical Use Case |
|---|---|---|
pre-commit | After staging, but before the commit is created | - Run linter - Check empty files - Format code |
prepare-commit-msg | When Git is generating the commit message | - Auto-fill commit templates or add issue IDs (Add Jira ID) - Insert default text based on branch |
commit-msg | After message is created, for validation | Enforce commit message conventions - Enforce format ( feat: msg)- Block WIP - Word min |
post-commit | After commit is completed | Send notifications or log commit details Show success message - Trigger tests - Notify team |
pre-push | Before push is executed | Run integration tests or prevent pushing to main |
post-merge | After a merge completes | Rebuild project, install dependencies, or notify team |
post-checkout | After switching branches/files | Set up environment, install packages, reset configs |
pre-rebase | Before rebase starts | Warn user or check conditions before continuing |
How to Use Git Hooks
Git provides sample hooks inside the .git/hooks folder. These files usually have .sample extensions. To activate a hook:
-
Remove the
.sampleextension. -
Make it executable:
chmod +x .git/hooks/pre-commit -
Edit the file and write your custom script (bash, Python, etc.).
Example: A simple pre-commit hook that runs terraform fmt and stages changes:
#!/bin/bash
terraform fmt -recursive
git ls-files '*.tf' | xargs git add
Conclusion
Git hooks offer powerful ways to automate routine tasks and enforce standards directly within your development workflow. By integrating hooks into your team practices, you can maintain cleaner codebases, catch issues earlier, and streamline collaboration. Whether you're linting code before every commit or ensuring no one pushes directly to production, Git hooks are a must-have for any professional developer or DevOps engineer.
Keep Learning 🚀
👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.
👉 My YouTube Channel – More videos, more fun, and lots of learning!
👉 📺 Watch this topic in video form
Stay curious, keep coding, and let’s make learning fun together! 🎉