A Git hook is a script that runs automatically at certain points in the Git workflow to automate tasks like checking code or sending notifications.
Click to reveal answer
beginner
Name two common types of Git hooks.
Pre-commit hook (runs before a commit is saved) and post-commit hook (runs after a commit is saved).
Click to reveal answer
beginner
How do Git hooks help automate workflows?
They run scripts automatically to check code quality, run tests, or update systems without manual steps, saving time and reducing errors.
Click to reveal answer
beginner
Where are Git hooks stored in a repository?
In the .git/hooks directory inside the Git repository.
Click to reveal answer
beginner
Can Git hooks prevent bad code from being committed?
Yes, pre-commit hooks can run checks and stop a commit if the code does not meet rules.
Click to reveal answer
What does a pre-commit Git hook do?
ACreates a new branch
BRuns after a commit is pushed to remote
CDeletes old commits automatically
DRuns before a commit is saved to check or modify code
✗ Incorrect
Pre-commit hooks run before saving a commit to check or modify the code.
Where do you place Git hook scripts?
Aany folder named hooks
B.git/hooks directory
Croot directory
Dremote repository
✗ Incorrect
Git hooks are stored in the .git/hooks directory inside the repository.
Which of these is NOT a benefit of Git hooks?
AAutomatically merge branches
BPrevent bad code commits
CAutomate repetitive tasks
DRun tests before committing
✗ Incorrect
Git hooks do not automatically merge branches.
What happens if a pre-commit hook script fails?
AThe branch is renamed
BThe commit proceeds anyway
CThe commit is stopped until issues are fixed
DThe repository is deleted
✗ Incorrect
If a pre-commit hook fails, it stops the commit to prevent bad code.
Which Git hook runs after a commit is made?
Apost-commit
Bpre-commit
Cpre-push
Dpost-merge
✗ Incorrect
The post-commit hook runs after a commit is made.
Explain how Git hooks help automate your coding workflow.
Think about tasks you do repeatedly before saving code.
You got /4 concepts.
Describe where Git hooks live and how you can use them to improve code quality.
Focus on location and purpose of hooks.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of Git hooks in a project?
easy
A. To create new branches automatically
B. To store large files outside the repository
C. To merge branches without conflicts
D. To automatically run scripts during Git actions
Solution
Step 1: Understand what Git hooks do
Git hooks are scripts that run automatically when certain Git events happen, like committing or pushing.
Step 2: Identify the main purpose
The main purpose is to automate tasks during Git actions, such as checks or notifications.
Final Answer:
To automatically run scripts during Git actions -> Option D
Quick Check:
Git hooks automate scripts = A [OK]
Hint: Hooks run scripts automatically on Git events [OK]
Common Mistakes:
Confusing hooks with branch management
Thinking hooks store files
Assuming hooks merge branches
2. Which of the following is the correct way to enable a Git hook script?
easy
A. Push the script to the remote repository
B. Add the script to the .gitignore file
C. Rename the script to remove the .sample extension in .git/hooks/
D. Create a new branch named hooks
Solution
Step 1: Locate Git hooks directory
Git hooks are stored in the .git/hooks/ folder, usually with .sample extensions.
Step 2: Enable a hook script
To enable a hook, you rename the script to remove the .sample extension so Git can run it.
Final Answer:
Rename the script to remove the .sample extension in .git/hooks/ -> Option C
Quick Check:
Remove .sample to enable hook = C [OK]
Hint: Remove .sample extension to activate hook scripts [OK]
Common Mistakes:
Adding hooks to .gitignore disables them
Pushing hooks doesn't enable them locally
Creating branches does not enable hooks
3. Given a pre-commit hook script that checks for TODO comments and rejects the commit if found, what will happen if you try to commit a file containing 'TODO: fix this'?
medium
A. The commit will be rejected with a warning about TODO comments
B. The commit will delete the TODO comments automatically
C. The commit will succeed but add a warning in the commit message
D. The commit will succeed without any message
Solution
Step 1: Understand the pre-commit hook behavior
The pre-commit hook runs before the commit is finalized and can reject commits based on conditions.
Step 2: Analyze the effect of TODO comments
If the script finds 'TODO' comments, it rejects the commit and shows a warning message.
Final Answer:
The commit will be rejected with a warning about TODO comments -> Option A
Quick Check:
Pre-commit rejects TODO commits = D [OK]
Hint: Pre-commit hooks can block commits with errors [OK]
Common Mistakes:
Assuming commit succeeds silently
Thinking commit adds warnings instead of blocking
Believing hooks modify code automatically
4. You wrote a post-commit hook script but it does not run after commits. What is the most likely reason?
medium
A. The script is named post-commit.sample
B. All of the above
C. The script file is missing execute permissions
D. The script is located outside the .git/hooks directory
Solution
Step 1: Check script permissions
Git requires hook scripts to have execute permissions to run.
Step 2: Check script name and location
The script must be named exactly (no .sample) and be inside .git/hooks/ folder.
Step 3: Combine all issues
Any of these issues (wrong name, wrong location, no execute permission) will prevent the hook from running.
Final Answer:
All of the above -> Option B
Quick Check:
Permissions, name, location all matter = D [OK]
Hint: Check permissions, name, and location for hooks [OK]
Common Mistakes:
Ignoring execute permissions
Leaving .sample extension
Placing script outside hooks folder
5. You want to automate sending a notification email after every successful push to the remote repository. Which Git hook should you use and why?
hard
A. post-push hook, because it runs after the push completes successfully
B. pre-push hook, because it runs before pushing and can stop the push
C. post-commit hook, because it runs after each commit locally
D. pre-commit hook, because it runs before committing changes
Solution
Step 1: Identify when to send notification
The notification should be sent after a successful push to the remote repository.
Step 2: Understand available hooks
Git does not have a built-in post-push hook, but the server-side post-receive hook runs after a successful push on the remote repository.
Step 3: Choose the best hook
To send notification after a successful push, use the server-side post-receive hook, which runs after the push completes successfully.
Final Answer:
post-push hook, because it runs after the push completes successfully -> Option A
Quick Check:
Use post-push (server-side post-receive) for notifications after push = A [OK]
Hint: Use post-receive hook on server to notify after push [OK]
Common Mistakes:
Assuming post-push hook exists locally in Git
Using pre-push which runs before push
Using post-commit which runs too early
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.