Bird
Raised Fist0
Gitdevops~20 mins

Client-side vs server-side hooks in Git - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Git Hooks Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Difference between client-side and server-side Git hooks

Which statement correctly describes the main difference between client-side and server-side Git hooks?

AClient-side hooks run on the developer's machine before or after local Git actions; server-side hooks run on the remote repository server during push or receive events.
BClient-side hooks run only on the remote server to validate commits; server-side hooks run locally to check code style.
CClient-side hooks are used to enforce branch policies on the server; server-side hooks modify files on the developer's machine.
DClient-side hooks are triggered after pushing code to the server; server-side hooks run before committing locally.
Attempts:
2 left
💡 Hint

Think about where the hooks execute: on your computer or on the remote server.

💻 Command Output
intermediate
1:30remaining
Output of a client-side pre-commit hook script

Given this simple pre-commit hook script in .git/hooks/pre-commit that prevents commits with TODO comments:

#!/bin/sh
grep -q 'TODO' **/*.py && echo 'Commit blocked: TODO found' && exit 1 || exit 0

What will happen if you try to commit a Python file containing a TODO comment?

AThe commit will succeed without any message.
BThe commit will be blocked and the message 'Commit blocked: TODO found' will be shown.
CThe script will cause a syntax error and the commit will fail silently.
DThe commit will succeed but print 'Commit blocked: TODO found' as a warning.
Attempts:
2 left
💡 Hint

Consider what grep -q does and the exit codes in shell scripts.

🔀 Workflow
advanced
2:00remaining
Server-side hook to reject pushes to main branch

You want to prevent anyone from pushing directly to the main branch on the remote Git server. Which server-side hook should you configure and what should it do?

AConfigure the <code>pre-push</code> hook on the client side to reject pushes to <code>main</code>.
BConfigure the <code>post-receive</code> hook to delete the <code>main</code> branch after the push completes.
CConfigure the <code>update</code> hook to allow all pushes except those to <code>refs/tags</code>.
DConfigure the <code>pre-receive</code> hook to check if the pushed ref is <code>refs/heads/main</code> and reject the push with a message.
Attempts:
2 left
💡 Hint

Think about which server-side hook runs before accepting pushed commits.

❓ Troubleshoot
advanced
1:30remaining
Why is a client-side commit-msg hook not running?

You created a commit-msg hook script in .git/hooks/ to validate commit messages, but it does not run when you commit. What is the most likely reason?

AThe hook script is named <code>commit-msg.sh</code> instead of <code>commit-msg</code>.
BThe hook script is in the wrong directory; it should be in <code>.git/hooks/commit-msg/</code>.
CThe hook script is not executable (missing execute permission).
DGit does not support <code>commit-msg</code> hooks.
Attempts:
2 left
💡 Hint

Check file permissions for scripts in .git/hooks.

✅ Best Practice
expert
2:30remaining
Best practice for sharing Git hooks across a team

What is the best way to ensure all developers on a team use the same Git hooks without manually copying scripts into each local .git/hooks directory?

AStore hooks in a version-controlled directory in the repository and configure Git to use that directory with <code>core.hooksPath</code>.
BAdd hook scripts to the <code>.git/hooks</code> directory and commit that directory to the repository.
CSend hook scripts by email to each developer to install manually.
DUse server-side hooks only and do not use client-side hooks.
Attempts:
2 left
💡 Hint

Think about how to version control hooks and configure Git to find them.

Practice

(1/5)
1. Which of the following best describes a client-side Git hook?
easy
A. A Git command to clone repositories.
B. A script that runs on the Git server to control repository access.
C. A tool to merge branches automatically on the server.
D. A script that runs on your local machine before or after Git actions like commit or push.

Solution

  1. Step 1: Understand client-side hooks context

    Client-side hooks run on your local computer and trigger before or after Git commands like commit or push.
  2. Step 2: Differentiate from server-side hooks

    Server-side hooks run on the Git server to enforce rules for all users, unlike client-side hooks which are local.
  3. Final Answer:

    A script that runs on your local machine before or after Git actions like commit or push. -> Option D
  4. Quick Check:

    Client-side hook = local script [OK]
Hint: Client-side hooks run locally before/after Git commands [OK]
Common Mistakes:
  • Confusing client-side hooks with server-side hooks
  • Thinking hooks run only on the server
  • Assuming hooks are Git commands
2. Which of the following is the correct location to place a client-side pre-commit hook script in a Git repository?
easy
A. /usr/local/git/hooks/pre-commit
B. .git/hooks/server-pre-commit
C. .git/hooks/pre-commit
D. .git/pre-commit

Solution

  1. Step 1: Identify the standard hooks directory

    Git client-side hooks are stored inside the .git/hooks directory of the local repository.
  2. Step 2: Confirm the correct hook filename

    The pre-commit hook script must be named exactly 'pre-commit' inside .git/hooks.
  3. Final Answer:

    .git/hooks/pre-commit -> Option C
  4. Quick Check:

    Client-side hooks live in .git/hooks [OK]
Hint: Client-side hooks go in .git/hooks folder [OK]
Common Mistakes:
  • Placing hooks outside the .git directory
  • Using wrong filenames for hooks
  • Confusing server-side hook locations
3. Given this server-side hook script named pre-receive on the Git server:
#!/bin/sh
while read oldrev newrev refname
 do
   if [ "$refname" = "refs/heads/main" ]; then
     echo "Pushes to main branch are not allowed."
     exit 1
   fi
 done
exit 0
What will happen if a user tries to push commits to the main branch?
medium
A. The push will be rejected with the message 'Pushes to main branch are not allowed.'
B. The push will succeed without any message.
C. The push will be accepted but with a warning.
D. The server will crash due to a syntax error.

Solution

  1. Step 1: Analyze the pre-receive hook logic

    The script reads each ref being pushed. If the ref is 'refs/heads/main', it prints a message and exits with status 1, which rejects the push.
  2. Step 2: Understand Git hook exit codes

    Exiting with 1 means failure, so Git rejects the push and shows the message to the user.
  3. Final Answer:

    The push will be rejected with the message 'Pushes to main branch are not allowed.' -> Option A
  4. Quick Check:

    Exit 1 in server hook rejects push [OK]
Hint: Exit code 1 in server hook blocks push [OK]
Common Mistakes:
  • Assuming push succeeds despite exit 1
  • Confusing client-side and server-side hook effects
  • Ignoring the printed message
4. You wrote a client-side pre-commit hook script but it does not run when you commit. What is the most likely reason?
medium
A. The script is placed in the wrong directory on the server.
B. The script file is not executable.
C. Git does not support pre-commit hooks.
D. You forgot to push the hook to the remote repository.

Solution

  1. Step 1: Check script permissions

    Client-side hooks must be executable files. If not, Git will ignore them silently.
  2. Step 2: Verify hook location and Git support

    The hook must be in .git/hooks locally. Git supports pre-commit hooks, and pushing hooks to remote does not affect local hooks.
  3. Final Answer:

    The script file is not executable. -> Option B
  4. Quick Check:

    Hooks must be executable to run [OK]
Hint: Make hook scripts executable with chmod +x [OK]
Common Mistakes:
  • Placing hooks on the server for client-side use
  • Thinking Git lacks pre-commit hooks
  • Expecting hooks to transfer via git push
5. You want to enforce that all commits pushed to the shared repository have a signed commit message. Which hook setup is best to ensure this rule is always checked regardless of the user's local setup?
hard
A. Implement a server-side pre-receive hook that rejects unsigned commits.
B. Ask each developer to add a client-side commit-msg hook to check signatures.
C. Use a client-side pre-push hook to warn about unsigned commits.
D. Rely on GitHub or GitLab UI to manually check commit signatures.

Solution

  1. Step 1: Understand enforcement scope

    Client-side hooks depend on each user's setup and can be bypassed or missing.
  2. Step 2: Use server-side hooks for mandatory checks

    Server-side hooks run on the shared repository and can reject pushes that don't meet rules, ensuring enforcement for all users.
  3. Final Answer:

    Implement a server-side pre-receive hook that rejects unsigned commits. -> Option A
  4. Quick Check:

    Server hooks enforce rules for all pushes [OK]
Hint: Use server-side hooks to enforce rules for all users [OK]
Common Mistakes:
  • Relying only on client-side hooks for enforcement
  • Assuming UI checks block pushes automatically
  • Ignoring that client hooks can be disabled
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