Skip to main content

Using Git hooks

Guide to using Git hooks in WordPress development. Learn how to automate quality control with pre-commit hooks and streamline your deployment (cache flushing, Gulp builds) using custom post-receive hooks in the Seravo environment.

Updated this week

Git hooks are scripts that allow you to run custom actions automatically during different stages of the Git workflow (e.g., before a commit or after a push). They reside in your repository's .git/hooks/ directory and are not automatically copied when pulling or pushing.

Learn more about Git hooks from Git documentation.

During Development: pre-commit hook

The pre-commit hook is executed locally on every commit attempt. If the hook returns a non-zero exit code, the commit is aborted. This is a powerful tool for quality control, preventing broken code or syntax errors from entering the repository.

Our WordPress project template includes an example hook in scripts/git-hooks/pre-commit. By default, it checks for PHP syntax errors (php -l) and can be extended to run automated tests.

How to skip the pre-commit hook

If you need to bypass the quality checks temporarily, use the -n flag:

git commit -n -m "commit message"

During Deployment: post-receive hook

When you run git push production, the remote server can trigger a post-receive hook. At Seravo, this hook is not pre-installed. If you want to automate tasks after a deployment, you must create the script yourself.

Common use cases for the post-receive hook:

  • Asset Building: Running npm install or gulp build.

  • Dependency Management: Running composer install if composer.json has changed.

  • Cache Management: Flushing WordPress and Nginx caches.

Setting up the post-receive hook

To make the hook active, the file must be located at /data/wordpress/.git/hooks/post-receive on the server and have executable permissions:

chmod +x /data/wordpress/.git/hooks/post-receive

Testing Git Hooks

Since Git hooks are regular shell scripts, you can test them by running them directly:

/data/wordpress/.git/hooks/post-receive

Example output of a well-configured script:

Seravo: running post-receive git hook
Seravo: composer.json was updated, installing...
Loading composer repositories with package information
Installing dependencies from lock file
Nothing to install or update
Seravo: SASS files changed, running Gulp...
[22:14:20] Finished 'sass' after 270 ms
Seravo: Flushing all caches...
----> Purging WordPress object cache...
Success: The cache was flushed. ---->
Purging Nginx page cache...
Cache purged successfully.
Did this answer your question?