Git — Hooks
What are Hooks?
Scripts that run automatically on Git events.
Hook Locations
.git/hooks/
├── pre-commit
├── commit-msg
├── pre-push
└── post-merge
Common Hooks
| Hook | When It Runs |
|---|---|
| pre-commit | Before commit |
| commit-msg | After commit message |
| pre-push | Before push |
| post-merge | After merge |
Pre-Commit Hook
#!/bin/sh
# .git/hooks/pre-commit
# Run linter
npm run lint
# Run tests
npm test
Commit Message Hook
#!/bin/sh
# .git/hooks/commit-msg
# Check message length
if [ $(cat $1 | wc -l) -lt 10 ]; then
echo "Commit message too short"
exit 1
fi
Make Hook Executable
chmod +x .git/hooks/pre-commit
Disable Hook
# Temporarily skip hooks
git commit --no-verify -m "Skip hooks"
Mini Practice
- Create a pre-commit hook
- Add validation
- Make hook executable
- Test the hook
Up Next
Continue with GitHub — GitHub platform.
Related Topics
Frequently Asked Questions about Hooks
What is Hooks in Git?
Hooks is a fundamental concept in Git. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Hooks?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Hooks.
Why is Hooks important in Git?
Hooks is essential for Git development. Understanding this concept will help you write better code and solve real-world problems more effectively.