Summary - Hooks
- Git course
- Advanced
- Lesson 24/56
- 5 min
Combine the hook flow to set up client-side checks.
Client-Side Hook Workflow
Git hooks are scripts that run locally before or after specific repository events. pre-commit performs file and code checks before a commit, while commit-msg validates the commit message. Both hooks can stop the commit operation by returning a failure status.
.git/hooks/pre-commit
.git/hooks/commit-msgPractice Create a Two-Stage Hook Validation
Define two separate Shell scripts. The first should represent the pre-commit task and return a successful exit code. The second should represent the commit-msg task and read the message file from the first argument. If the message starts with feat:, exit successfully; otherwise return a failure status.
Starter code
# pre-commit betiği
# Başarılı çıkış
# commit-msg betiği
# Mesajı oku
# feat: kontrolü yapSample solution
# pre-commit
pre_commit() {
exit 0
}
# commit-msg
message=$(cat "$1")
case "$message" in
feat:*) exit 0 ;;
*) echo "Geçersiz mesaj"; exit 1 ;;
esacExpected output
Geçersiz mesaj
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 In what order do the two hooks run during a commit?
- First commit-msg, then pre-commit
- First pre-commit, then commit-msg Correct answer
- Both run only after the commit
- Both run only during push
Explanation: pre-commit first checks files and code. Then commit-msg validates the message of the commit being created.
2 Complete the Responsibilities of Both Hooks
pre-commit
commit-msg
____
pre-commit
commit-msg
validation
Explanation: pre-commit and commit-msg perform different validations at different stages. Their shared purpose is to prevent errors during the commit process.
3 Hook Validation Workflow
| Term | Meaning |
|---|---|
pre-commit |
Code and file checks |
commit-msg |
Message format check |
exit 0 |
Validation succeeded |
exit 1 |
Blocks the commit operation |