0
0
Gitdevops~5 mins

Client-side vs server-side hooks in Git - CLI Comparison

Choose your learning style9 modes available
Introduction
Git hooks are scripts that run automatically at certain points in your work. Client-side hooks run on your computer before or after you make changes, while server-side hooks run on the central server to control what happens when you push changes.
When you want to check your code for errors before saving it locally.
When you want to prevent bad code from being pushed to the shared repository.
When you want to automatically format your code before committing.
When you want to send notifications after someone pushes code to the server.
When you want to enforce rules like commit message style on the server.
Commands
Navigate to the hooks directory inside your Git repository where client-side hook scripts are stored.
Terminal
cd my-repo/.git/hooks
Expected OutputExpected
No output (command runs silently)
List all the example hook scripts available by default in the hooks folder.
Terminal
ls
Expected OutputExpected
applypatch-msg.sample commit-msg.sample post-commit.sample post-checkout.sample post-merge.sample pre-commit.sample pre-push.sample pre-rebase.sample prepare-commit-msg.sample update.sample
Create a simple client-side pre-commit hook script that prints a message before a commit is made.
Terminal
echo '#!/bin/sh\necho "Running pre-commit hook"' > pre-commit
Expected OutputExpected
No output (command runs silently)
Make the pre-commit hook script executable so Git can run it automatically.
Terminal
chmod +x pre-commit
Expected OutputExpected
No output (command runs silently)
Make a commit to see the client-side pre-commit hook run and print its message.
Terminal
git commit -m "Test commit"
Expected OutputExpected
Running pre-commit hook [main abc1234] Test commit 1 file changed, 1 insertion(+)
Check the server-side hooks folder on the Git server to see available hook scripts.
Terminal
ssh user@server 'cd /path/to/repo.git/hooks && ls'
Expected OutputExpected
pre-receive post-receive update post-update
Create a simple server-side pre-receive hook that prints a message when someone pushes code.
Terminal
ssh user@server 'echo "#!/bin/sh\necho \"Server-side pre-receive hook running\"" > /path/to/repo.git/hooks/pre-receive && chmod +x /path/to/repo.git/hooks/pre-receive'
Expected OutputExpected
No output (command runs silently)
Push your changes to the server to trigger the server-side pre-receive hook and see its message.
Terminal
git push origin main
Expected OutputExpected
Server-side pre-receive hook running Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Delta compression using up to 4 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To ssh://server/path/to/repo.git abc1234..def5678 main -> main
Key Concept

Client-side hooks run on your computer before or after you make changes, while server-side hooks run on the central server to control what happens when you push changes.

Common Mistakes
Not making the hook script executable
Git will ignore hook scripts that are not executable, so they won't run.
Always run chmod +x on your hook scripts to make them executable.
Editing sample hook files without removing the .sample extension
Git only runs hook scripts without the .sample extension, so your changes won't take effect.
Remove the .sample extension or create a new file without it for your hook.
Trying to run server-side hooks locally
Server-side hooks only run on the Git server during push operations, so running them locally has no effect.
Manage server-side hooks by accessing the Git server directly.
Summary
Client-side hooks run scripts on your local machine before or after Git actions like commit or push.
Server-side hooks run on the Git server to control or respond to pushes from clients.
You must make hook scripts executable and place them in the correct hooks folder to work.