What if your code could check itself before you even notice a mistake?
Creating custom hook scripts in Git - Why You Should Know This
Imagine you have a team working on a project, and everyone needs to follow certain rules before saving their work. Without automation, each person has to remember to check these rules manually every time they make changes.
Manually checking rules is slow and easy to forget. Mistakes slip through, causing bugs and delays. It's like proofreading a long essay by eye every time--tiring and error-prone.
Custom hook scripts automatically run checks or actions at key moments, like before saving or sharing code. This means rules are enforced without anyone needing to remember, saving time and avoiding errors.
Remember to run tests before every commit
Create a pre-commit hook script that runs tests automatically
It enables teams to keep code quality high and consistent without extra effort.
A developer writes a script that prevents committing code if tests fail, so broken code never reaches the shared project.
Manual checks are slow and unreliable.
Custom hooks automate important tasks at the right time.
This keeps projects healthy and teams efficient.
Practice
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 DQuick Check:
Git hooks = automatic scripts at Git events [OK]
- Thinking hooks manually change commit messages
- Confusing hooks with branch deletion
- Assuming hooks create repositories
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 commandchmod +xadds execute permission without changing other permissions.Final Answer:
chmod +x .git/hooks/pre-commit -> Option AQuick Check:
Make hook executable = chmod +x [OK]
- Using git commands that don't exist for hooks
- Setting overly permissive permissions like 777
- Trying to enable hooks via git config
.git/hooks/pre-commit and made executable:
#!/bin/sh if git diff --cached --quiet; then echo "No changes staged for commit." exit 1 fiWhat happens when you try to commit without staging any changes?
Solution
Step 1: Analyze the script logic
The script checks if there are no staged changes usinggit 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 AQuick Check:
Exit 1 in pre-commit = block commit [OK]
- Assuming commit proceeds despite exit 1
- Confusing staged vs unstaged changes
- Expecting syntax errors from correct shell script
#!/bin/sh echo "Commit done!"Which is the most likely reason?
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 BQuick Check:
Hooks must be executable to run [OK]
- Placing scripts outside .git/hooks directory
- Assuming echo is not allowed in hooks
- Using wrong hook filenames
.git/hooks/commit-msg will achieve this?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 CQuick Check:
Exit 1 if 'JIRA' missing blocks commit [OK]
- Not exiting with non-zero to block commit
- Checking for presence instead of absence
- Only printing message without blocking
Ready to prove it?
Pass the test and this pattern moves unknown
