0
0
Gitdevops~30 mins

Client-side vs server-side hooks in Git - Hands-On Comparison

Choose your learning style9 modes available
Understanding Client-side vs Server-side Git Hooks
📖 Scenario: You are working on a team project using Git for version control. Your team wants to automate checks to improve code quality and consistency. You will learn how to set up simple client-side and server-side Git hooks to automate tasks before commits and before pushing code to the shared repository.
🎯 Goal: Build a basic Git setup with a client-side pre-commit hook that checks commit messages and a server-side pre-receive hook that rejects pushes with empty commit messages.
📋 What You'll Learn
Create a client-side pre-commit hook script
Create a server-side pre-receive hook script
Understand where to place client-side and server-side hooks
Test the hooks by making commits and pushes
💡 Why This Matters
🌍 Real World
Teams use Git hooks to automate code quality checks and enforce rules before code is committed or pushed.
💼 Career
Understanding Git hooks is important for developers and DevOps engineers to maintain code standards and automate workflows.
Progress0 / 4 steps
1
Set up a client-side pre-commit hook
Create a file named pre-commit inside the .git/hooks/ directory of your local repository. Add a script that checks if the commit message is not empty by reading the commit message file .git/COMMIT_EDITMSG. The script should exit with status 1 if the message is empty, preventing the commit.
Git
Need a hint?

The pre-commit hook runs before the commit is finalized. Use shell script syntax to check if the commit message file is empty.

2
Set up a server-side pre-receive hook
Create a file named pre-receive inside the hooks/ directory of the remote bare Git repository. Add a script that reads commit messages from the pushed commits and rejects the push if any commit message is empty by exiting with status 1.
Git
Need a hint?

The pre-receive hook runs on the server before accepting pushed commits. Use git rev-list to get commits and check their messages.

3
Make a commit to test the client-side hook
Create a commit with an empty commit message in your local repository to test that the client-side pre-commit hook prevents the commit.
Git
Need a hint?

Use git commit --allow-empty -m "" to create a commit with an empty message and trigger the pre-commit hook.

4
Push commits to test the server-side hook
Push your commits to the remote repository to test that the server-side pre-receive hook rejects pushes with empty commit messages. Use git push and observe the error message.
Git
Need a hint?

Run git push and check the output for the rejection message from the server-side hook.