Bird
Raised Fist0
Gitdevops~20 mins

pre-push hook in Git - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pre-Push Hook Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this pre-push hook script?
Given this simple pre-push hook script, what will be the output when you try to push?
Git
#!/bin/sh

echo "Running pre-push checks..."
exit 1
APush is blocked with message: pre-push hook failed
BPush is blocked with message: Running pre-push checks...
CPush succeeds silently with no output
DPush succeeds but prints Running pre-push checks...
Attempts:
2 left
💡 Hint
Remember that exit 1 in a hook stops the push.
🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of a pre-push hook in Git?
Choose the best description of what a pre-push hook does.
AIt runs before pushing to validate or block the push
BIt automatically merges branches before pushing
CIt runs after the push completes to notify the user
DIt deletes remote branches after pushing
Attempts:
2 left
💡 Hint
Think about when the hook runs relative to the push action.
❓ Troubleshoot
advanced
2:30remaining
Why does this pre-push hook not block the push?
This pre-push hook script is supposed to block pushes if tests fail, but pushes always succeed. Why?
Git
#!/bin/sh

./run_tests.sh
exit 0
AGit hooks cannot block pushes
BThe run_tests.sh script is not executable
CThe script always exits with 0, so push is never blocked
DThe hook file is not named correctly
Attempts:
2 left
💡 Hint
Check the exit code of the hook script.
🔀 Workflow
advanced
1:30remaining
In which directory should you place a pre-push hook script?
Where do you put a pre-push hook script so Git runs it automatically?
A.git/hooks/pre-push
Broot directory of the project
Cany directory named hooks anywhere
D.git/pre-push
Attempts:
2 left
💡 Hint
Git looks for hooks in a specific folder inside the .git directory.
✅ Best Practice
expert
3:00remaining
What is the best way to share a pre-push hook with your team?
You want all team members to use the same pre-push hook. What is the best practice?
AUse a global Git hook configured on each developer's machine
BAsk each member to manually create the hook in their .git/hooks folder
CPut the hook script in the root directory and rely on Git to find it automatically
DCommit the hook script inside the repository and add a setup script to copy it to .git/hooks
Attempts:
2 left
💡 Hint
Git does not track .git/hooks folder contents.

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