Requirements — Git Hook Collection¶
What you will build and what you need before starting.
Learning Objectives¶
- Understand the Git hook lifecycle and which hooks fire when
- Write a
pre-commithook that runs ShellCheck and rejects unsafe scripts - Write a
commit-msghook that enforces a commit message convention - Write a
pre-pushhook that blocks force-pushes tomain - Install hooks with a setup script so the whole team can use them
What You Will Build¶
A collection of three production-quality Git hooks:
| Hook | Fires | Purpose |
|---|---|---|
pre-commit |
Before commit is created | Run ShellCheck on staged .sh files; reject commits with trailing whitespace |
commit-msg |
After message is written | Enforce type: subject format (conventional commits) |
pre-push |
Before remote push | Block force-pushes to main; warn on large file additions |
Plus an installer script that symlinks hooks into .git/hooks/.
How Git Hooks Work¶
Git runs hook scripts at specific points in its workflow. Hooks live in .git/hooks/. A hook must be:
- Named exactly (e.g.,
pre-commit, no extension) - Executable (
chmod +x) - Return exit code 0 to allow the operation, non-zero to abort it
# Hook locations
.git/hooks/pre-commit ← runs before creating a commit
.git/hooks/commit-msg ← runs after the message is written
.git/hooks/pre-push ← runs before git push sends data
Because .git/ is not committed, hooks must be installed by each developer. The installer in this project automates that.
Prerequisites¶
| Skill | Where to review |
|---|---|
| Exit codes | Week 02 Day 02 Part 1 — Error Handling |
git basics |
Week 02 Day 04 Part 1 — Automation Tools |
| ShellCheck | Week 02 Day 05 Part 2 — Best Practices |
| Regex with grep | Week 02 Day 03 Part 2 — Regular Expressions |
| Functions | Week 02 Day 01 Part 1 |
Commit Message Convention¶
The commit-msg hook enforces this format:
<type>: <subject>
[optional body]
Valid types: feat, fix, docs, style, refactor, test, chore, ci, build
Examples:
feat: add user authentication
fix: handle empty input in parser
docs: update installation instructions
Invalid (hook will reject):
Fixed some stuff ← no type prefix
WIP ← no colon separator
feat:add space missing ← no space after colon
Time Estimate¶
| Part | Time |
|---|---|
| Setup + hook skeleton | 15 min |
| pre-commit: ShellCheck | 20 min |
| pre-commit: trailing whitespace | 15 min |
| commit-msg validator | 20 min |
| pre-push: main protection | 20 min |
| install.sh script | 15 min |
| Total | ~105 min |