Bird
Raised Fist0
Gitdevops~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a pre-push hook in Git?
easy
A. To delete local branches after pushing
B. To merge branches automatically before pushing
C. To run checks or tests before code is pushed to a remote repository
D. To create a backup of the repository before pushing

Solution

  1. Step 1: Understand the role of pre-push hooks

    Pre-push hooks run scripts before pushing code to a remote repository to catch errors early.
  2. Step 2: Identify the correct purpose

    They help ensure code quality by running tests or checks before the push happens.
  3. Final Answer:

    To run checks or tests before code is pushed to a remote repository -> Option C
  4. Quick Check:

    Pre-push hook = run checks before push [OK]
Hint: Pre-push hooks run before push to catch problems early [OK]
Common Mistakes:
  • Confusing pre-push with post-push hooks
  • Thinking it merges branches automatically
  • Assuming it deletes branches
2. Which of the following is the correct way to make a pre-push hook script executable on Linux or macOS?
easy
A. chmod +x .git/hooks/pre-push
B. git enable pre-push
C. chmod 777 .git/hooks/pre-push
D. git hook activate pre-push

Solution

  1. Step 1: Identify how to make scripts executable

    On Linux/macOS, chmod +x sets execute permission on a file.
  2. Step 2: Apply to pre-push hook

    Running chmod +x .git/hooks/pre-push makes the hook script runnable by Git.
  3. Final Answer:

    chmod +x .git/hooks/pre-push -> Option A
  4. Quick Check:

    Make hook executable = chmod +x [OK]
Hint: Use chmod +x to make hook scripts executable [OK]
Common Mistakes:
  • Using git commands that don't exist for hooks
  • Setting overly permissive permissions like 777
  • Forgetting to make the script executable
3. Given this pre-push hook script snippet, what will happen if the tests fail?
#!/bin/sh
npm test || exit 1
medium
A. The push will continue regardless of test results
B. The script will delete the remote branch
C. The tests will be skipped
D. The push will be blocked and stopped

Solution

  1. Step 1: Understand the script logic

    The script runs npm test. If tests fail, npm test returns non-zero, triggering exit 1.
  2. Step 2: Effect of exit 1 in pre-push hook

    Exiting with a non-zero code stops the push process, blocking the push.
  3. Final Answer:

    The push will be blocked and stopped -> Option D
  4. Quick Check:

    Exit 1 in pre-push = block push [OK]
Hint: Exit non-zero in pre-push stops the push [OK]
Common Mistakes:
  • Assuming push continues even if tests fail
  • Thinking tests are skipped automatically
  • Confusing exit codes with success
4. You wrote a pre-push hook script but it does not run when you push. What is the most likely cause?
medium
A. The remote repository is down
B. The script is missing execute permissions
C. You forgot to commit your changes
D. The branch name is incorrect

Solution

  1. Step 1: Check script execution requirements

    Git requires hook scripts to have execute permission to run them.
  2. Step 2: Identify common cause of hook not running

    If the script lacks execute permission, Git silently skips running it.
  3. Final Answer:

    The script is missing execute permissions -> Option B
  4. Quick Check:

    Missing execute permission = hook won't run [OK]
Hint: Check execute permission if hook doesn't run [OK]
Common Mistakes:
  • Blaming remote repository for local hook issues
  • Thinking commit status affects hook execution
  • Assuming branch name affects hook running
5. You want your pre-push hook to run tests only on files changed in the last commit before pushing. Which approach is best?
hard
A. Use git diff --name-only HEAD~1 HEAD inside the hook to get changed files and run tests accordingly
B. Run tests on all files every time without checking changes
C. Manually list files in the hook script each time
D. Push first, then run tests on the remote repository

Solution

  1. Step 1: Identify how to detect changed files

    The command git diff --name-only HEAD~1 HEAD lists files changed in the last commit.
  2. Step 2: Use this command in pre-push hook

    Using this inside the hook lets you run tests only on changed files, saving time and resources.
  3. Final Answer:

    Use git diff --name-only HEAD~1 HEAD inside the hook to get changed files and run tests accordingly -> Option A
  4. Quick Check:

    Detect changed files with git diff for selective tests [OK]
Hint: Use git diff to find changed files for selective tests [OK]
Common Mistakes:
  • Running tests on all files unnecessarily
  • Manually updating file lists each time
  • Running tests after push instead of before
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard