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-commit
  • prepare-commit-msg
  • commit-msg
  • post-commit
  • pre-push
  • post-merge
  • post-checkout
  • pre-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-receive
  • update
  • post-receive
  • post-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 NameWhen It RunsTypical Use Case
pre-commitAfter staging, but before the commit is created- Run linter
- Check empty files
- Format code
prepare-commit-msgWhen Git is generating the commit message- Auto-fill commit templates or add issue IDs (Add Jira ID)
- Insert default text based on branch
commit-msgAfter message is created, for validationEnforce commit message conventions
- Enforce format (feat: msg)
- Block WIP
- Word min
post-commitAfter commit is completedSend notifications or log commit details
Show success message
- Trigger tests
- Notify team
pre-pushBefore push is executedRun integration tests or prevent pushing to main
post-mergeAfter a merge completesRebuild project, install dependencies, or notify team
post-checkoutAfter switching branches/filesSet up environment, install packages, reset configs
pre-rebaseBefore rebase startsWarn 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:

  1. Remove the .sample extension.

  2. Make it executable:

    chmod +x .git/hooks/pre-commit
    
  3. 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! 🎉