0
0
Gitdevops~20 mins

pre-commit hook in Git - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pre-commit Hook Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a simple pre-commit hook script
What will be the output when you try to commit with this pre-commit hook script in .git/hooks/pre-commit?
Git
#!/bin/sh

echo "Checking commit message length..."
MSG=$(head -n1 .git/COMMIT_EDITMSG)
LEN=$(echo -n "$MSG" | wc -c)
if [ $LEN -lt 10 ]; then
  echo "Commit message too short. Please write at least 10 characters."
  exit 1
fi
exit 0
ACommit is blocked with message: "Commit message too short. Please write at least 10 characters."
BCommit proceeds but prints "Commit message too short. Please write at least 10 characters."
CCommit is blocked with message: "Checking commit message length..." only
DCommit proceeds without any message
Attempts:
2 left
💡 Hint
Think about what happens if the commit message is shorter than 10 characters.
🧠 Conceptual
intermediate
1:30remaining
Purpose of pre-commit hooks in Git
Which of the following best describes the main purpose of a pre-commit hook in Git?
ATo run checks or scripts before a commit is finalized
BTo automatically merge branches before committing
CTo push commits to the remote repository automatically
DTo delete untracked files before committing
Attempts:
2 left
💡 Hint
Think about what happens right before a commit is saved.
Troubleshoot
advanced
2:00remaining
Why does the pre-commit hook not run?
You created a pre-commit hook script in .git/hooks/pre-commit but it does not run when committing. What is the most likely reason?
AThe commit message is empty
BThe script file is named <code>pre-commit.sh</code> instead of <code>pre-commit</code>
CThe script file is missing execute permissions
DThe Git repository is not initialized
Attempts:
2 left
💡 Hint
Check the file permissions of the hook script.
🔀 Workflow
advanced
2:30remaining
Order of operations in a pre-commit hook script
You want to create a pre-commit hook that first checks code style with flake8 and then runs tests with pytest. Which order of commands in the script ensures the commit is blocked if either check fails?
Git
#!/bin/sh

# Commands to run
A
2
3
1
B
1
2
3
C
3
2
1
D
2
1
3
Attempts:
2 left
💡 Hint
The script should stop at the first failure to block the commit.
Best Practice
expert
3:00remaining
Best practice for sharing pre-commit hooks across a team
What is the best practice to ensure all team members use the same pre-commit hooks without manually copying scripts into their .git/hooks directories?
AAdd the hook scripts to the repository and instruct everyone to copy them manually
BUse a tool like <code>pre-commit</code> framework to manage hooks via configuration files
CCommit the <code>.git/hooks</code> directory to the repository
DSend the hook scripts by email to each team member
Attempts:
2 left
💡 Hint
Think about automation and consistency for teams.