πͺ Git Hooks β Automate Git Tasks with Custom Scripts
π§² Introduction β Why Use Git Hooks?
Ever wanted to automatically format code, run tests, or reject commits that donβt meet naming conventions? With Git Hooks, you can automate tasks at various points in the Git workflow.
Git hooks are scripts that run automatically in response to specific Git events like commits, merges, and pushes. They help enforce policies, reduce manual errors, and streamline development workflows.
π― In this guide, you’ll learn:
- What Git hooks are
- Types of Git hooks (client-side & server-side)
- How to configure and write hook scripts
- Common use cases
- Best practices
π What Are Git Hooks?
Git hooks are executable scripts located in a special .git/hooks/
directory inside a Git repository. Each script corresponds to a specific Git event (e.g., pre-commit
, post-merge
).
These scripts:
- Are triggered automatically
- Can be written in Bash, Python, or any scripting language
- Can block Git actions (e.g., prevent bad commits) or just log data
π§° Types of Git Hooks
πΉ 1. Client-Side Hooks
Run on your local machine during Git operations.
Hook Name | When It Runs | Purpose |
---|---|---|
pre-commit | Before git commit | Lint, test, format, check commit contents |
prepare-commit-msg | Before commit message editor opens | Auto-fill commit messages |
commit-msg | After commit message is entered | Validate or enforce commit message rules |
post-commit | After a successful commit | Send logs, display notifications |
pre-push | Before git push to remote | Run tests, verify branches |
post-merge | After a successful git merge | Install dependencies, update config |
πΉ 2. Server-Side Hooks
Run on the Git server during push operations.
Hook Name | When It Runs | Purpose |
---|---|---|
pre-receive | Before changes are accepted by server | Reject unauthorized or invalid pushes |
update | Once per branch update | Restrict branch updates |
post-receive | After push is received by server | Trigger CI/CD pipelines, notify systems |
π οΈ How to Configure Git Hooks
β Step 1: Locate the hooks folder
Inside your Git repo:
cd .git/hooks
Youβll find sample files like pre-commit.sample
.
β Step 2: Create or edit a hook script
Example: pre-commit
to block commits with TODOs:
#!/bin/bash
if grep -r "TODO" .; then
echo "β Commit blocked: TODO found in code."
exit 1
fi
β Step 3: Make the script executable
chmod +x .git/hooks/pre-commit
The script will now run automatically before each commit.
π‘ Common Git Hook Use Cases
Task | Hook Used |
---|---|
Auto-run unit tests before commit | pre-commit |
Enforce commit message format | commit-msg |
Auto-install dependencies after pull | post-merge |
Prevent pushing to main | pre-push |
Trigger CI/CD deployment | post-receive |
π¦ Sharing Git Hooks with Teams
Hooks in .git/hooks/
arenβt version controlled by default.
π Workaround:
- Store hooks in a shared folder (e.g.,
.githooks/
) - Configure Git to use it:
git config core.hooksPath .githooks
Now Git will look for hooks in .githooks/
instead of .git/hooks/
.
π§ Pro Tip β Use Prebuilt Hook Managers
- Husky (for JavaScript projects): Easy Git hook management via
package.json
- lefthook: Fast and language-agnostic Git hook manager
- pre-commit framework: Python-based with many community hooks
β οΈ Common Mistakes
- β Forgetting
chmod +x
β Hooks wonβt run - β Not sharing hooks β Team members miss automation
- β Writing complex scripts without testing β Unexpected failures
β Best Practices
- Keep hook scripts simple and fast
- Fail early (use
exit 1
to stop faulty commits or pushes) - Store hooks in project repo and sync via
core.hooksPath
- Use hook managers (like Husky) in large teams or CI/CD environments
π Summary β Git Hooks
Git Hooks are powerful tools to automate, validate, and secure your Git workflow. From preventing bad commits to triggering deployments, they help maintain consistent standards across teams and projects.
π Key Takeaways:
- Git hooks are scripts triggered by Git events (commit, push, merge, etc.)
- They can run validations, tests, formatting, or trigger deployments
- Use
core.hooksPath
to make hooks shareable and team-friendly
βοΈ Real-World Relevance: Every DevOps or developer team can benefit from hooksβwhether itβs linting code, blocking poor commits, or kicking off deployments.
βGit Hooks β Frequently Asked Questions (FAQs)
β Where are Git hooks stored by default?
β
Inside .git/hooks/
directory of each Git repo.
β How do I prevent a commit if a condition fails?
β
Use a pre-commit
hook with exit 1
to block the commit.
β Can Git hooks be version controlled?
β
Not by default. But you can store them in .githooks/
and use:
git config core.hooksPath .githooks
β What language can I write Git hooks in?
β
Any executable language (Bash, Python, Node.js, etc.) as long as the file is marked executable.
β Can I use Git hooks in CI/CD pipelines?
β
Yes. Server-side hooks like post-receive
can trigger deployment scripts and integration workflows.
Share Now :