0
0
Gitdevops~10 mins

commit-msg hook for message validation in Git - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - commit-msg hook for message validation
Start commit-msg hook
Read commit message file
Validate message format
Allow commit
Show error message
Exit hook
The commit-msg hook runs automatically during commit. It reads the commit message, checks if it matches rules, then allows or rejects the commit.
Execution Sample
Git
#!/bin/sh
MSG_FILE=$1
MSG=$(cat "$MSG_FILE")

if echo "$MSG" | grep -qE '^JIRA-[0-9]+: .{10,}'
then
  exit 0
else
  echo "Error: Commit message must start with JIRA-<number>: and be at least 10 chars"
  exit 1
fi
This hook script checks if the commit message starts with 'JIRA-' followed by a number and a colon, then has at least 10 characters.
Process Table
StepActionCommit Message ContentValidation ResultHook Exit CodeOutput
1Read commit message fileJIRA-123: Fix bug in loginN/AN/A
2Check pattern '^JIRA-[0-9]+: .{10,}'JIRA-123: Fix bug in loginMatches patternN/A
3Exit hookN/AValid message0
4Commit allowedN/AN/AN/ACommit proceeds
5Read commit message fileFix login bugN/AN/A
6Check pattern '^JIRA-[0-9]+: .{10,}'Fix login bugDoes not matchN/A
7Print error messageN/AInvalid messageN/AError: Commit message must start with JIRA-<number>: and be at least 10 chars
8Exit hookN/AInvalid message1
9Commit rejectedN/AN/AN/ACommit aborted
💡 Hook exits with 0 if message valid, 1 if invalid, controlling commit success
Status Tracker
VariableStartAfter Step 1After Step 2After Step 6Final
MSG_FILEunset'.git/COMMIT_EDITMSG''.git/COMMIT_EDITMSG''.git/COMMIT_EDITMSG''.git/COMMIT_EDITMSG'
MSGunset'JIRA-123: Fix bug in login' or 'Fix login bug''JIRA-123: Fix bug in login' or 'Fix login bug''Fix login bug''Fix login bug' or 'JIRA-123: Fix bug in login'
Validation ResultunsetN/AMatches pattern or Does not matchDoes not matchMatches pattern or Does not match
Exit CodeunsetN/A0 or 110 or 1
Key Moments - 3 Insights
Why does the commit get rejected even if the message looks descriptive?
The hook requires the message to start with 'JIRA-' followed by a number and a colon, then at least 10 characters. Messages without this prefix fail validation as shown in rows 6-9 of the execution table.
What does exit code 0 or 1 mean in the hook?
Exit code 0 means the commit message passed validation and the commit proceeds (rows 3-4). Exit code 1 means validation failed and the commit is aborted (rows 8-9).
How does the hook read the commit message to validate it?
The hook reads the commit message file passed as the first argument ($1) and stores its content in MSG variable (row 1). It then applies the pattern check on MSG.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the exit code when the commit message is 'JIRA-123: Fix bug in login'?
A2
B1
C0
DNo exit code
💡 Hint
Check row 3 in the execution table where the message matches the pattern and exit code is set.
At which step does the hook print an error message for an invalid commit message?
AStep 7
BStep 4
CStep 2
DStep 1
💡 Hint
Look for the step where output shows the error message in the execution table.
If the commit message was 'JIRA-45: Add tests', how would the validation result change?
AIt would match the pattern and pass
BIt would fail because message is too short
CIt would fail because no JIRA number
DIt would pass without checks
💡 Hint
Refer to the regex pattern requiring at least 10 characters after the prefix, see execution table row 2.
Concept Snapshot
commit-msg hook syntax:
#!/bin/sh
MSG_FILE=$1
MSG=$(cat "$MSG_FILE")

Validation:
Use regex to check MSG format
Exit 0 to allow commit
Exit 1 to reject commit

Purpose: enforce commit message rules automatically
Full Transcript
The commit-msg hook runs automatically when you make a git commit. It reads the commit message from a file passed as an argument. Then it checks if the message matches a pattern: it must start with 'JIRA-' followed by a number and a colon, and have at least 10 characters. If the message matches, the hook exits with code 0, allowing the commit. If not, it prints an error message and exits with code 1, stopping the commit. This helps keep commit messages consistent and meaningful.