What if your project could catch mistakes before they cause headaches for everyone?
Client-side vs server-side hooks in Git - When to Use Which
Imagine you and your friends are working on a shared project. Every time someone makes a change, they have to remember to check if their code follows the rules before sharing it. Sometimes they forget, and mistakes sneak in.
Relying on everyone to manually check their work is slow and risky. Mistakes can break the project, and fixing them later wastes time and causes frustration.
Client-side and server-side hooks automatically check and enforce rules at different stages. Client-side hooks catch issues before changes leave your computer, while server-side hooks protect the shared project by checking changes before they are accepted.
Remember to run tests and style checks before commitUse a client-side hook to run tests automatically before commit
This makes teamwork smoother and safer by catching problems early and keeping the shared project healthy.
A developer tries to commit code with a typo. The client-side hook stops the commit and asks to fix it. Another developer pushes code to the server, but the server-side hook rejects it because tests fail, preventing broken code from entering the project.
Manual checks are slow and error-prone.
Client-side hooks catch problems before code leaves your computer.
Server-side hooks protect the shared project by validating incoming changes.
Practice
Solution
Step 1: Understand client-side hooks context
Client-side hooks run on your local computer and trigger before or after Git commands like commit or push.Step 2: Differentiate from server-side hooks
Server-side hooks run on the Git server to enforce rules for all users, unlike client-side hooks which are local.Final Answer:
A script that runs on your local machine before or after Git actions like commit or push. -> Option DQuick Check:
Client-side hook = local script [OK]
- Confusing client-side hooks with server-side hooks
- Thinking hooks run only on the server
- Assuming hooks are Git commands
Solution
Step 1: Identify the standard hooks directory
Git client-side hooks are stored inside the .git/hooks directory of the local repository.Step 2: Confirm the correct hook filename
The pre-commit hook script must be named exactly 'pre-commit' inside .git/hooks.Final Answer:
.git/hooks/pre-commit -> Option CQuick Check:
Client-side hooks live in .git/hooks [OK]
- Placing hooks outside the .git directory
- Using wrong filenames for hooks
- Confusing server-side hook locations
pre-receive on the Git server:
#!/bin/sh
while read oldrev newrev refname
do
if [ "$refname" = "refs/heads/main" ]; then
echo "Pushes to main branch are not allowed."
exit 1
fi
done
exit 0
What will happen if a user tries to push commits to the main branch?Solution
Step 1: Analyze the pre-receive hook logic
The script reads each ref being pushed. If the ref is 'refs/heads/main', it prints a message and exits with status 1, which rejects the push.Step 2: Understand Git hook exit codes
Exiting with 1 means failure, so Git rejects the push and shows the message to the user.Final Answer:
The push will be rejected with the message 'Pushes to main branch are not allowed.' -> Option AQuick Check:
Exit 1 in server hook rejects push [OK]
- Assuming push succeeds despite exit 1
- Confusing client-side and server-side hook effects
- Ignoring the printed message
pre-commit hook script but it does not run when you commit. What is the most likely reason?Solution
Step 1: Check script permissions
Client-side hooks must be executable files. If not, Git will ignore them silently.Step 2: Verify hook location and Git support
The hook must be in .git/hooks locally. Git supports pre-commit hooks, and pushing hooks to remote does not affect local hooks.Final Answer:
The script file is not executable. -> Option BQuick Check:
Hooks must be executable to run [OK]
- Placing hooks on the server for client-side use
- Thinking Git lacks pre-commit hooks
- Expecting hooks to transfer via git push
Solution
Step 1: Understand enforcement scope
Client-side hooks depend on each user's setup and can be bypassed or missing.Step 2: Use server-side hooks for mandatory checks
Server-side hooks run on the shared repository and can reject pushes that don't meet rules, ensuring enforcement for all users.Final Answer:
Implement a server-sidepre-receivehook that rejects unsigned commits. -> Option AQuick Check:
Server hooks enforce rules for all pushes [OK]
- Relying only on client-side hooks for enforcement
- Assuming UI checks block pushes automatically
- Ignoring that client hooks can be disabled
Ready to prove it?
Pass the test and this pattern moves unknown
