AlgoMaster Logo

Server-Side Hooks

10 min readUpdated December 6, 2025
Listen to this chapter
Unlock Audio

Server-side hooks in Git are powerful tools that enable you to enforce policies, automate workflows, and enhance collaboration among team members.

While client-side hooks allow developers to catch errors before they commit code, server-side hooks operate on the repository level, triggering actions during specific events like pushes and merges.

This chapter delves into the world of server-side hooks, detailing their configuration, use cases, and best practices, so you can use them effectively in your projects.

Understanding Server-Side Hooks

Server-side hooks are scripts that Git executes on the server when certain repository events occur. They help maintain code quality and enforce policies across all clients interacting with the repository. Server-side hooks include:

  • pre-receive: Runs before any refs are updated.
  • update: Executes for each branch being updated.
  • post-receive: Triggers after the refs have been updated.

These hooks are stored in the .git/hooks directory of the bare repository. While you can create and edit these files, they do not come with default scripts like client-side hooks. Therefore, you must implement the logic you want to execute.

Setting Up Server-Side Hooks

To set up server-side hooks, you first need access to the Git server. This could be a self-hosted instance or a managed Git service that allows custom hooks. In a bare repository, the hooks reside in the .git/hooks directory.

Here is a step-by-step approach to set up a basic pre-receive hook.

Navigate to the hooks directory of your bare repository:

Create a new file named pre-receive:

Open the file in your favorite text editor and add the following script:

Make the script executable:

This simple script checks if there is an attempt to delete the main branch and rejects the push if so. Understanding how each event works is crucial for crafting effective hooks.

Using the update Hook

The update hook is called once for each branch that is being updated. This gives you the ability to apply specific checks to individual branches. For instance, if you want to ensure that only certain users can push to the release branch, you can implement an update hook like this:

In the hooks directory, create an update file:

Add the following code to enforce user permissions:

Make the file executable:

This hook allows only specified users to push to the release branch, preventing unauthorized changes. By tailoring hooks to specific branches, you can enforce different rules based on your project’s workflow.

Implementing the post-receive Hook

The post-receive hook runs after the push is successfully completed. This is an excellent opportunity to automate tasks such as deploying code or notifying team members. Here's an example of how to send a notification after a successful push.

Create a post-receive file in the hooks directory:

Edit the file with the following script:

Make it executable:

This hook sends a Slack notification for every push to the repository. The post-receive hook can help keep your team informed and streamline the development process.

Common Pitfalls and Best Practices

While working with server-side hooks, several pitfalls can arise. Here are some best practices to avoid common issues:

  • Test Hooks Locally: Before deploying hooks on a production server, test them locally in a non-critical environment. This practice helps catch syntax errors and logic flaws.
  • Log Outputs: Use logging to track what your hooks are doing. Redirect outputs to a log file to debug issues:
  • Keep Hooks Lightweight: Hooks should be quick to execute. Long-running tasks can delay pushes and lead to frustration. Offload heavy operations to background jobs or external services.
  • Use Environment Variables: If your scripts rely on certain configurations or credentials, consider using environment variables for flexibility.
  • Backup Hooks: Regularly back up your hooks, especially if they contain critical logic for your workflows. This ensures you can restore functionality quickly if needed.

Real-World Use Cases

Server-side hooks have several practical applications that can enhance your development workflow:

  1. Code Quality Checks: You can integrate tools like linters or test frameworks into your hooks to enforce coding standards. For instance, using a pre-receive hook to run a linter can prevent poor-quality code from being pushed.
  2. Deployment Automation: Automate deployments with the post-receive hook by triggering scripts that deploy the latest code to staging or production environments, reducing manual errors.
  3. Branch Policies: Enforce branch policies by using update hooks to restrict who can push to critical branches, ensuring that only approved team members can make changes.
  4. Notifications: Use post-receive hooks to notify team members of important events, such as merges or releases, keeping everyone in the loop.

By leveraging server-side hooks effectively, you can create a more robust and reliable development environment.

Now that you understand server-side hooks and how to implement them, you are ready to explore Git Attributes.

In the next chapter, we will look at how Git Attributes can help customize behavior for different files and directories, enhancing your version control experience.