0
0
Gitdevops~10 mins

pre-push hook in Git - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - pre-push hook
User runs git push
Git checks for pre-push hook script
Yes
Run pre-push hook script
Hook passes
Push proceeds
End
When you run 'git push', Git looks for a pre-push hook script. If found, it runs it. If the script passes, push continues; if it fails, push stops.
Execution Sample
Git
#!/bin/sh
# pre-push hook example

if ! ./run-tests.sh; then
  echo "Tests failed, push aborted."
  exit 1
fi
exit 0
This pre-push hook runs tests before allowing a push. If tests fail, it stops the push.
Process Table
StepActionScript OutputExit CodePush Status
1User runs 'git push'Waiting for hooks
2Git finds pre-push hook scriptRunning hook
3Hook runs './run-tests.sh'Tests passed0Hook passes
4Hook exits with 00Push proceeds
5Push completes successfullyPush done
💡 Hook exits with 0, so push proceeds and completes
Status Tracker
VariableStartAfter Step 3After Step 4Final
Exit CodeN/A000
Push StatusWaitingRunning hookPush proceedsPush done
Key Moments - 3 Insights
Why does the push stop if the hook script exits with a non-zero code?
Git treats any non-zero exit code from the pre-push hook as a failure, so it blocks the push to prevent bad code from being pushed. See execution_table step 3 for exit code effect.
What happens if there is no pre-push hook script?
Git skips the hook and pushes immediately. The hook only runs if the script file exists and is executable.
Can the hook script show messages to the user?
Yes, any output like 'Tests failed, push aborted.' is shown in the terminal to explain why the push stopped (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the exit code of the hook script at step 3?
A0
B1
C255
DNo exit code
💡 Hint
Check the 'Exit Code' column at step 3 in the execution_table.
At which step does the push actually proceed?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look for 'Push proceeds' in the 'Push Status' column.
If the tests fail and the hook exits with code 1, what would happen?
APush proceeds anyway
BGit ignores the hook
CPush is blocked
DPush completes but with warnings
💡 Hint
Recall that non-zero exit codes block the push as explained in key_moments.
Concept Snapshot
pre-push hook is a Git script run before pushing.
If it exits 0, push continues.
If non-zero, push stops.
Use it to run tests or checks.
Place script in .git/hooks/pre-push and make executable.
Full Transcript
When you run 'git push', Git looks for a pre-push hook script in the .git/hooks directory. If it finds one, it runs the script before pushing. The script can run tests or checks. If the script exits with 0, Git continues the push. If it exits with a non-zero code, Git stops the push to prevent bad changes from being sent. The script can print messages to explain why the push stopped. If no hook script exists, Git pushes immediately. This helps keep code quality high by stopping pushes that fail tests or checks.