A pre-push hook is a script that runs automatically before you push your changes to a remote repository. It can check your code or run tests to stop bad changes from being pushed.
Click to reveal answer
beginner
Where do you place a pre-push hook script in a Git project?
You place the pre-push hook script inside the .git/hooks/ folder of your project, and name it pre-push (no file extension).
Click to reveal answer
beginner
What must you do to make a pre-push hook script executable?
You must give the script execute permission using chmod +x .git/hooks/pre-push so Git can run it automatically.
Click to reveal answer
intermediate
What happens if a pre-push hook script exits with a non-zero status?
Git will stop the push process, preventing your changes from being sent to the remote repository.
Click to reveal answer
beginner
Give an example use case for a pre-push hook.
A pre-push hook can run automated tests or check code style before pushing. If tests fail, the push is stopped to keep the remote code clean.
Click to reveal answer
Where is the pre-push hook script located in a Git project?
A.git/pre-push
Broot/pre-push
C.git/hooks/pre-push
Dhooks/pre-push
✗ Incorrect
The pre-push hook script must be placed in the .git/hooks/ directory with the exact name pre-push.
What does a pre-push hook do if it exits with status 1?
AAllows the push
BStops the push
CDeletes the remote branch
DCreates a new commit
✗ Incorrect
If the pre-push hook exits with a non-zero status like 1, Git stops the push to prevent unwanted changes.
Which command makes a pre-push hook script executable?
Agit enable pre-push
Bgit hook activate pre-push
Cchmod 777 pre-push
Dchmod +x .git/hooks/pre-push
✗ Incorrect
You use chmod +x on the hook script to give it execute permission so Git can run it.
When does the pre-push hook run?
ABefore pushing changes to remote
BAfter pushing changes to remote
CBefore committing changes
DAfter merging branches
✗ Incorrect
The pre-push hook runs automatically right before Git sends your changes to the remote repository.
Which of these is a common use for a pre-push hook?
ARun tests before pushing
BCreate a new branch
CMerge branches automatically
DDelete local commits
✗ Incorrect
Running tests before pushing helps catch errors early and keep the remote code stable.
Explain what a pre-push hook is and why it is useful.
Think about what happens right before you send your code to others.
You got /4 concepts.
Describe the steps to create and enable a pre-push hook in a Git project.
Focus on file location, permissions, and script behavior.
You got /4 concepts.
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
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.
Step 2: Identify the correct purpose
They help ensure code quality by running tests or checks before the push happens.
Final Answer:
To run checks or tests before code is pushed to a remote repository -> Option C
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
Step 1: Identify how to make scripts executable
On Linux/macOS, chmod +x sets execute permission on a file.
Step 2: Apply to pre-push hook
Running chmod +x .git/hooks/pre-push makes the hook script runnable by Git.
Final Answer:
chmod +x .git/hooks/pre-push -> Option A
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
Step 1: Understand the script logic
The script runs npm test. If tests fail, npm test returns non-zero, triggering exit 1.
Step 2: Effect of exit 1 in pre-push hook
Exiting with a non-zero code stops the push process, blocking the push.
Final Answer:
The push will be blocked and stopped -> Option D
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
Step 1: Check script execution requirements
Git requires hook scripts to have execute permission to run them.
Step 2: Identify common cause of hook not running
If the script lacks execute permission, Git silently skips running it.
Final Answer:
The script is missing execute permissions -> Option B
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
Step 1: Identify how to detect changed files
The command git diff --name-only HEAD~1 HEAD lists files changed in the last commit.
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.
Final Answer:
Use git diff --name-only HEAD~1 HEAD inside the hook to get changed files and run tests accordingly -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.