0
0
Gitdevops~10 mins

Client-side vs server-side hooks in Git - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Process Flow - Client-side vs server-side hooks
Start: Git Action Triggered
Run Client-side Hook Script
Hook Passes?
Run Server-side Hook Script
Hook Passes?
Action Proceeds
Reject Action
When a Git action happens, client-side hooks run first on your computer. If they pass, server-side hooks run on the server. If all hooks pass, the action completes.
Execution Sample
Git
# Client-side pre-commit hook
#!/bin/sh
# Check for TODO comments
if git diff --cached | grep TODO; then
  echo "Remove TODOs before commit"
  exit 1
fi

# Server-side pre-receive hook
#!/bin/sh
while read oldrev newrev refname; do
  if [ "$refname" = "refs/heads/main" ]; then
    # Reject force pushes
    if [ "$oldrev" != "0000000000000000000000000000000000000000" ] && ! git merge-base --is-ancestor "$oldrev" "$newrev"; then
      echo "Force pushes to main are not allowed"
      exit 1
    fi
  fi
done
exit 0
This example shows a client-side pre-commit hook that blocks commits with TODO comments, and a server-side pre-receive hook that rejects force pushes to main branch.
Process Table
StepHook TypeAction TriggeredHook Script Runs?Hook ResultNext Step
1Client-sidegit commitYesNo TODO found, passRun server-side hooks
2Server-sideReceive pushYesNo force push detected, passAction proceeds
3Client-sidegit commitYesTODO found, failReject commit
4Client-sidegit commitYesNo TODO found, passRun server-side hooks
5Server-sideReceive pushYesForce push detected, failReject push
💡 Execution stops when a hook fails or all hooks pass allowing the action.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
Commit ContentInitialNo TODONo TODOHas TODONo TODONo TODO
Push TypeInitialN/ANormal pushN/AN/AForce push
Hook Pass StatusN/APassPassFailPassFail
Key Moments - 3 Insights
Why does the commit get rejected even before reaching the server?
Because the client-side pre-commit hook found TODO comments and failed, as shown in execution_table row 3.
What happens if the client-side hook passes but the server-side hook fails?
The push is rejected by the server-side hook, as shown in execution_table row 5, stopping the action on the server.
Are server-side hooks run for local commits?
No, server-side hooks run only on the server during push or receive actions, not on local commits (see execution_table rows 1 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the hook result at step 3?
ANo TODO found, pass
BTODO found, fail
CForce push detected, fail
DNo force push detected, pass
💡 Hint
Check the 'Hook Result' column in execution_table row 3.
At which step does the server-side hook reject the action?
AStep 2
BStep 3
CStep 5
DStep 1
💡 Hint
Look for 'Force push detected, fail' in the 'Hook Result' column.
If the client-side hook finds no TODOs, what is the next step?
ARun server-side hooks
BReject commit immediately
CSkip server-side hooks
DAbort the git action
💡 Hint
See execution_table row 1 'Next Step' column.
Concept Snapshot
Client-side hooks run on your computer before actions like commit.
Server-side hooks run on the server during push or receive.
Client-side hooks can block bad commits early.
Server-side hooks enforce rules centrally.
If any hook fails, the action is stopped.
Hooks run in order: client-side first, then server-side.
Full Transcript
When you perform a Git action like commit or push, Git checks for hooks to run. First, client-side hooks run on your local machine. For example, a pre-commit hook can check your code for TODO comments and stop the commit if any are found. If the client-side hooks pass, and you push to a remote repository, server-side hooks run on the server. For example, a pre-receive hook can reject force pushes to the main branch. If any hook fails, Git stops the action and shows an error. This process helps catch problems early on your computer and enforce rules on the server. The execution table shows steps where hooks run and their results, helping you understand when and why actions proceed or stop.