📖 Scenario: You are working on a team project using Git. Your team wants to ensure that every commit message includes the word "JIRA" followed by a ticket number. To help with this, you will create a custom Git hook script that checks commit messages before they are saved.
🎯 Goal: Build a Git commit-msg hook script that checks if the commit message contains the word "JIRA" followed by a ticket number. If the message does not match, the commit should be stopped with a clear message.
📋 What You'll Learn
Create a Git hook script file named commit-msg in the .git/hooks directory
Add a check in the script to verify the commit message contains the pattern JIRA- followed by digits
Make the script executable
Test the hook by attempting a commit with and without the required pattern
💡 Why This Matters
🌍 Real World
Teams often use Git hooks to enforce rules and automate checks before code changes are saved. This helps keep code quality high and consistent.
💼 Career
Knowing how to create and use Git hooks is valuable for developers and DevOps engineers to automate workflows and enforce team policies.
Progress0 / 4 steps
1
Create the Git hook script file
Create a file named commit-msg inside the .git/hooks directory with the first line #!/bin/sh to specify the shell interpreter.
Git
Hint
The first line of a shell script should be #!/bin/sh to tell Git which shell to use.
2
Add a variable to read the commit message file path
Add a line to the script that assigns the first argument $1 to a variable named commit_msg_file.
Git
Hint
The commit message file path is passed as the first argument to the script. Save it in a variable for later use.
3
Add the check for the JIRA ticket pattern
Add an if statement that uses grep -qE to check if the commit message file contains the pattern JIRA-[0-9]+. If the pattern is not found, print Commit message must include a JIRA ticket like JIRA-123 and exit with status 1.
Git
Hint
Use grep -qE 'JIRA-[0-9]+' to quietly check for the pattern. If not found, print a message and stop the commit with exit 1.
4
Make the script executable and test it
Run chmod +x .git/hooks/commit-msg to make the script executable. Then run git commit -m "Test commit without JIRA ticket" and observe the output.
Git
Hint
Use chmod +x to make the script executable. When you try to commit without a JIRA ticket, the script should stop the commit and show the message.
Practice
(1/5)
1. What is the main purpose of creating a custom Git hook script?
easy
A. To delete branches remotely
B. To change the Git commit message manually
C. To create new Git repositories
D. To run scripts automatically at specific Git events
Solution
Step 1: Understand Git hooks
Git hooks are scripts that run automatically at certain points in Git's workflow, like before a commit or after a push.
Step 2: Identify the purpose of custom hooks
Custom hooks let you automate tasks or enforce rules during these Git events without manual intervention.
Final Answer:
To run scripts automatically at specific Git events -> Option D
Quick Check:
Git hooks = automatic scripts at Git events [OK]
Hint: Hooks run scripts automatically at Git events [OK]
Common Mistakes:
Thinking hooks manually change commit messages
Confusing hooks with branch deletion
Assuming hooks create repositories
2. Which of the following is the correct way to make a Git hook script executable on Linux or macOS?
easy
A. chmod +x .git/hooks/pre-commit
B. git hook enable pre-commit
C. chmod 777 .git/hooks/pre-commit
D. git config hook.pre-commit true
Solution
Step 1: Understand file permissions for hooks
Git hooks must be executable files to run automatically.
Step 2: Use the correct command to make a file executable
The command chmod +x adds execute permission without changing other permissions.
Final Answer:
chmod +x .git/hooks/pre-commit -> Option A
Quick Check:
Make hook executable = chmod +x [OK]
Hint: Use chmod +x to make hook scripts executable [OK]
Common Mistakes:
Using git commands that don't exist for hooks
Setting overly permissive permissions like 777
Trying to enable hooks via git config
3. Given this pre-commit hook script placed in .git/hooks/pre-commit and made executable:
#!/bin/sh
if git diff --cached --quiet; then
echo "No changes staged for commit."
exit 1
fi
What happens when you try to commit without staging any changes?
medium
A. The commit is blocked with message 'No changes staged for commit.'
B. The commit proceeds normally without any message
C. The commit is blocked with a syntax error
D. The commit proceeds but shows a warning
Solution
Step 1: Analyze the script logic
The script checks if there are no staged changes using git diff --cached --quiet. If true, it prints a message and exits with code 1.
Step 2: Understand Git hook exit codes
Exiting with code 1 in a pre-commit hook stops the commit process and shows the message.
Final Answer:
The commit is blocked with message 'No changes staged for commit.' -> Option A
Quick Check:
Exit 1 in pre-commit = block commit [OK]
Hint: Exit non-zero in pre-commit stops commit [OK]
Common Mistakes:
Assuming commit proceeds despite exit 1
Confusing staged vs unstaged changes
Expecting syntax errors from correct shell script
4. You wrote this post-commit hook script but it does not run after commits:
#!/bin/sh
echo "Commit done!"
Which is the most likely reason?
medium
A. The script filename is incorrect
B. The script is not executable
C. Git hooks cannot echo messages
D. The script must be in the project root, not .git/hooks
Solution
Step 1: Check common hook issues
Hooks must be executable files inside .git/hooks/ with correct names.
Step 2: Identify the most common cause
Often, scripts don't run because they lack execute permission, even if named correctly.
Final Answer:
The script is not executable -> Option B
Quick Check:
Hooks must be executable to run [OK]
Hint: Make hook scripts executable with chmod +x [OK]
Common Mistakes:
Placing scripts outside .git/hooks directory
Assuming echo is not allowed in hooks
Using wrong hook filenames
5. You want to create a custom Git hook that prevents commits if the commit message does not contain the word 'JIRA'. Which script placed in .git/hooks/commit-msg will achieve this?
hard
A. #!/bin/sh
if grep -q 'JIRA' "$1"; then
exit 1
fi
C. #!/bin/sh
if ! grep -q 'JIRA' "$1"; then
echo "Commit message must include 'JIRA'."
exit 1
fi
D. #!/bin/sh
echo "Checking commit message for JIRA"
Solution
Step 1: Understand commit-msg hook argument
The commit message file path is passed as the first argument ($1) to the script.
Step 2: Check script logic for enforcing 'JIRA'
#!/bin/sh
if ! grep -q 'JIRA' "$1"; then
echo "Commit message must include 'JIRA'."
exit 1
fi uses grep -q to check if 'JIRA' is missing, then prints a message and exits with 1 to block the commit.
Step 3: Evaluate other options
#!/bin/sh
grep 'JIRA' "$1" || echo "Commit message missing JIRA" only echoes but does not block commit. #!/bin/sh
if grep -q 'JIRA' "$1"; then
exit 1
fi blocks if 'JIRA' is present (wrong logic). #!/bin/sh
echo "Checking commit message for JIRA" just echoes without checks.
Final Answer:
#!/bin/sh
if ! grep -q 'JIRA' "$1"; then
echo "Commit message must include 'JIRA'."
exit 1
fi -> Option C
Quick Check:
Exit 1 if 'JIRA' missing blocks commit [OK]
Hint: Use commit-msg hook with exit 1 to block bad messages [OK]
Common Mistakes:
Not exiting with non-zero to block commit
Checking for presence instead of absence
Only printing message without blocking
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.