Skip to main content

Using Git Hooks

Learn how to automate code quality checks with a pre-commit hook and post-deployment tasks (such as Composer installs and cache purging) with a post-receive hook in Seravo's environment.

Git hooks are scripts that let you run automated actions at specific stages of your Git workflow—such as before committing changes (commit) or immediately after deploying code to the server (push).

Hook scripts reside in the local .git/hooks/ directory of each Git repository. Because the contents of .git/ are not tracked by version control, hooks are not copied automatically during git clone, git pull, or git push operations. To make sharing hooks easier, Seravo's WordPress project template stores sample scripts under version control in scripts/git-hooks/, where they can be easily enabled.

Prerequisites

Before setting up Git hooks, ensure you have:

  • A local Git clone of your project: pre-commit hooks are installed locally inside your workstation's .git/hooks/ directory.

  • SSH access to the server: Creating a deployment post-receive hook and setting executable permissions (chmod +x) require SSH access to /data/wordpress/.git/hooks/ on the server.

During Development: The pre-commit Hook

A local pre-commit hook runs on your workstation every time you execute git commit. If the script detects an issue and exits with a non-zero status code, the commit is aborted. This prevents errors—such as PHP syntax mistakes—from ever entering your Git history.

The sample script in Seravo's project template (scripts/git-hooks/pre-commit) automatically checks modified PHP files for syntax errors (php -l) before allowing a commit.

Enabling the pre-commit Hook

Enable the sample script in your local project directory by copying it to .git/hooks/ and granting execution permissions:

cp scripts/git-hooks/pre-commit .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

Tip: Alternatively, you can configure Git to use the version-controlled hooks directory directly by running git config core.hooksPath scripts/git-hooks in your local repository.

Temporarily Bypassing the pre-commit Hook

If you need to commit changes while temporarily skipping local checks, pass the --no-verify flag (or -n for short):

git commit --no-verify -m "Work in progress"

During Deployment: The post-receive Hook

When you deploy changes to the server using git push production master, Git on the server can automatically execute a post-receive hook immediately after receiving the updated files. On Seravo, this hook is not enabled by default and can be configured per project on the server.

Common tasks automated with a post-receive hook include:

  • Installing dependencies: Running composer install --no-dev when composer.json is used.

  • Building frontend assets: Compiling theme CSS and JavaScript files (such as npm run build or gulp build).

  • Purging caches: Flushing the WordPress object cache and Nginx page cache so updates become visible to visitors right away.

Enabling the post-receive Hook on the Server

Connect to the server via SSH. If your project is based on Seravo's WordPress project template, copy the sample script into place and make it executable:

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

Alternatively, you can create a streamlined custom script at /data/wordpress/.git/hooks/post-receive:

#!/bin/bash
set -e

echo "Running post-receive deployment tasks..."

# Install Composer dependencies
if [ -f "/data/wordpress/composer.json" ]; then
cd /data/wordpress && composer install --no-dev --optimize-autoloader
fi

# Flush WordPress object cache and Nginx page cache
wp cache flush --path=/data/wordpress/htdocs
wp-purge-cache

Remember to grant execution permissions to your custom script:

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

Testing Git Hooks

Because Git hooks are standard shell scripts, you can test them directly from the command line without creating a new commit or triggering a push.

Test the server's post-receive hook over SSH by running:

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

Example output of a successful run:

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.

Similarly, you can test your local pre-commit hook from your project directory by running .git/hooks/pre-commit.

Common Issues and Troubleshooting

  • The hook script does not run at all during git commit or git push: Verify that the filename is spelled accurately with no file extension (pre-commit or post-receive) and that the file has execution permissions (chmod +x).

  • The hook fails with bad interpreter: No such file or directory: The file was likely saved with Windows line endings (CRLF). Convert the line endings to Unix format (LF) using sed -i 's/\r$//' /data/wordpress/.git/hooks/post-receive.

  • Hooks stopped working after cloning the repository onto a new machine: Git does not copy the contents of .git/hooks/ during git clone. Copy the hook from scripts/git-hooks/ into .git/hooks/ again and make it executable (or configure git config core.hooksPath scripts/git-hooks).

  • git push succeeded even though a command inside post-receive failed: The post-receive hook runs after Git has already received the pushed commits. Review the remote: error output printed during git push, fix the issue on the server, and re-run /data/wordpress/.git/hooks/post-receive to complete the deployment tasks.

See Also

Git hooks help maintain consistent code quality and reliably automate routine deployment steps. If you need assistance setting up hook scripts or configuring deployment automation, our support team is happy to help at [email protected].

Did this answer your question?