A commit-msg hook is a script that runs automatically after a commit message is entered but before the commit is finalized. It checks the commit message to ensure it meets certain rules or formats.
Click to reveal answer
beginner
Where do you place a commit-msg hook script in a Git repository?
You place the commit-msg hook script inside the .git/hooks/ directory of your repository, and it must be executable.
Click to reveal answer
intermediate
What happens if a commit-msg hook script exits with a non-zero status?
If the commit-msg hook script exits with a non-zero status, Git stops the commit process and shows an error message. This prevents the commit from being saved.
Click to reveal answer
beginner
Give an example of a simple rule a commit-msg hook might enforce.
A simple rule could be that the commit message must start with a capital letter and be at least 10 characters long.
Click to reveal answer
beginner
How can you test a commit-msg hook script?
You can test it by trying to make a commit with different messages and seeing if the hook allows or blocks the commit based on your rules.
Click to reveal answer
Where is the commit-msg hook script located in a Git repository?
A.git/hooks/
B.git/commit-msg/
Croot directory
Dany directory
✗ Incorrect
The commit-msg hook script must be placed inside the .git/hooks/ directory.
What does a non-zero exit code from a commit-msg hook script do?
AAutomatically fixes the commit message
BAllows the commit to proceed
CDeletes the commit message
DStops the commit and shows an error
✗ Incorrect
A non-zero exit code stops the commit process and shows an error message.
Which of these is a valid use of a commit-msg hook?
ARun unit tests
BCheck commit message format
CPush to remote repository
DMerge branches
✗ Incorrect
The commit-msg hook is used to check the commit message format before committing.
How do you make a commit-msg hook script executable?
ANo action needed
BRename it to <code>commit-msg.sh</code>
CRun <code>chmod +x .git/hooks/commit-msg</code>
DPlace it in the root directory
✗ Incorrect
You must make the script executable with chmod +x.
What file does the commit-msg hook receive as input?
AThe file containing the commit message
BThe staged files
CThe repository config file
DThe last commit hash
✗ Incorrect
The hook receives the path to the file containing the commit message as its first argument.
Explain how a commit-msg hook can help maintain commit message quality.
Think about how automatic checks can stop bad messages.
You got /4 concepts.
Describe the steps to create and enable a commit-msg hook for message validation.
Focus on script location, permissions, and testing.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a commit-msg hook in Git?
easy
A. To check and validate the commit message before the commit is saved
B. To automatically push commits to the remote repository
C. To merge branches after a commit
D. To delete old commits from history
Solution
Step 1: Understand the role of Git hooks
Git hooks are scripts that run at specific points in Git's workflow to automate tasks or checks.
Step 2: Identify the function of the commit-msg hook
The commit-msg hook runs after a commit message is entered but before the commit is finalized, allowing validation of the message.
Final Answer:
To check and validate the commit message before the commit is saved -> Option A
Quick Check:
commit-msg hook = validate commit message [OK]
Hint: commit-msg hook validates commit messages before saving [OK]
Common Mistakes:
Confusing commit-msg hook with pre-push or post-commit hooks
Thinking it automatically pushes commits
Assuming it modifies commit history
2. Which of the following is the correct way to make a commit-msg hook executable on a Unix-like system?
easy
A. chmod 000 .git/hooks/commit-msg
B. chmod +x .git/hooks/commit-msg
C. git commit --hook commit-msg
D. git hook enable commit-msg
Solution
Step 1: Understand file permissions for hooks
Git hooks are scripts stored in the .git/hooks directory and must be executable to run.
Step 2: Identify the correct command to make a file executable
The command chmod +x adds execute permission to the file, enabling the hook to run.
Final Answer:
chmod +x .git/hooks/commit-msg -> Option B
Quick Check:
chmod +x = make executable [OK]
Hint: Use chmod +x to enable hook execution [OK]
Common Mistakes:
Using chmod 000 which removes all permissions
Trying git commands that don't exist for hooks
Not setting executable permission, so hook won't run
3. Given this commit-msg hook script snippet, what will happen if the commit message is empty?
#!/bin/sh
if [ -z "$1" ]; then
echo "Error: Commit message is empty." >&2
exit 1
fi
medium
A. The commit will be blocked with an error message
B. The commit message will be replaced with a default message
C. The commit will proceed without any message
D. The script will ignore the message and exit with 0
Solution
Step 1: Analyze the script condition
The script checks if the first argument $1 is empty using [ -z "$1" ]. In the commit-msg hook, $1 is the path to a temporary file containing the proposed commit message.
Step 2: Understand Git's behavior
Since $1 is the file path, it is never empty, so the condition [ -z "$1" ] is false. However, the script as written incorrectly checks the variable $1 instead of the content of the file. To check if the commit message is empty, the script should check the content of the file, e.g., if [ ! -s "$1" ] or if [ -z "$(cat $1)" ]. As is, the script will not block empty commit messages.
Final Answer:
The commit will proceed without any message -> Option C
Quick Check:
$1 is file path, not content; checking it for emptiness is incorrect [OK]
Hint: $1 is file path, not content; check with `cat $1` [OK]
Common Mistakes:
Thinking exit 1 allows commit to proceed
Assuming the message is replaced automatically
Confusing $1 with the commit message text instead of file path
4. You wrote this commit-msg hook to reject messages shorter than 10 characters:
#!/bin/sh
MSG=$(cat "$1")
if [ ${#MSG} -lt 10 ]; then
echo "Commit message too short." >&2
exit 1
fi
But it does not block short messages. What is the likely problem?
medium
A. The hook script is not executable
B. The commit message file path is incorrect
C. The shell does not support ${#MSG} syntax for string length
D. The script should use -z instead of -lt
Solution
Step 1: Check the script logic
The script correctly reads the commit message and checks its length using ${#MSG} which is valid in sh/bash.
Step 2: Consider common hook issues
If the hook is not executable, Git will ignore it and allow commits without running the script.
Final Answer:
The hook script is not executable -> Option A
Quick Check:
Non-executable hook = script not run [OK]
Hint: Make sure hook script is executable with chmod +x [OK]
Common Mistakes:
Assuming ${#MSG} is invalid in shell
Confusing file path with message content
Using wrong test operators like -z for length comparison
5. You want to enforce commit messages to start with a ticket number like PROJ-123: followed by a space and description. Which commit-msg hook snippet correctly validates this pattern?
hard
A. #!/bin/sh
MSG=$(cat "$1")
if echo "$MSG" | grep -qE '^PROJ-[a-z]+: .+'; then
echo "Invalid ticket format" >&2
exit 1
fi
B. #!/bin/sh
MSG=$(cat "$1")
if echo "$MSG" | grep -q '^PROJ-[0-9]+: .+'; then
exit 1
fi
C. #!/bin/sh
if [ -z "$1" ]; then
echo "Empty message" >&2
exit 0
fi
D. #!/bin/sh
MSG=$(cat "$1")
if ! echo "$MSG" | grep -qE '^PROJ-[0-9]+: .+'; then
echo "Commit message must start with 'PROJ-<number>: '" >&2
exit 1
fi
Solution
Step 1: Understand the required pattern
The commit message must start with 'PROJ-' followed by digits, a colon, a space, then some text.
Step 2: Analyze each option's regex and logic
#!/bin/sh
MSG=$(cat "$1")
if ! echo "$MSG" | grep -qE '^PROJ-[0-9]+: .+'; then
echo "Commit message must start with 'PROJ-<number>: '" >&2
exit 1
fi uses grep -qE with '^PROJ-[0-9]+: .+' and exits with error if pattern not matched, which is correct. #!/bin/sh
MSG=$(cat "$1")
if echo "$MSG" | grep -q '^PROJ-[0-9]+: .+'; then
exit 1
fi exits on match (wrong logic). #!/bin/sh
if [ -z "$1" ]; then
echo "Empty message" >&2
exit 0
fi exits 0 on empty message (wrong). #!/bin/sh
MSG=$(cat "$1")
if echo "$MSG" | grep -qE '^PROJ-[a-z]+: .+'; then
echo "Invalid ticket format" >&2
exit 1
fi checks for lowercase letters after PROJ- which is incorrect.
Final Answer:
#!/bin/sh
MSG=$(cat "$1")
if ! echo "$MSG" | grep -qE '^PROJ-[0-9]+: .+'; then
echo "Commit message must start with 'PROJ-<number>: '" >&2
exit 1
fi -> Option D