Post

Git Hooks

Git hooks provide a mechanism to execute code automatically at certain points in the Git workflow.

The hooks can be any executable code, including PowerShell, Python, or other scripts.

In this post, we will explore Git Hooks, their installation, types, practical use-cases, and seamless sharing Hooks with your team.

Installing Git Hooks

Hooks reside within the .git/hooks directory of every Git repository. Upon initializing a Git repository, this directory is automatically populated with example scripts.

These samples are appended with the .sample extension to ensure they remain inactive by default.

To install a hook, simply eliminate the .sample extension from the filename, or craft a new script from scratch without the .sample extension.

Samples

Types of Git Hooks

There are two types of Git hooks

  1. Client-side Hooks: Executed locally before or after Git operations on the developer’s machine.
  2. Server-side Hooks: Triggered on the remote server in response to specific events.

Examples of Client-side Hooks:

  • pre-commit
  • prepare-commit-msg
  • commit-msg
  • post-commit

Examples of Server-side Hooks:

  • pre-receive
  • update
  • post-receive

Client Side & Server Side Hooks

Client Side & Server Side Hooks

Practical use cases for using Git Hooks

There are several practical use cases for employing Git hooks in your development workflow, you can do anything with Git hooks.

Here are some use-case examples:

  • Code Quality Enforcement
  • Commit Message Standards
  • Security Checks
  • Sending notifications to your team’s chat room (Teams, Slack, etc.)

Example:

This is a commit-msg hook that verifies a commit message includes a work item ID in a specific format [ID: <work item ID>]

If the commit message doesn’t match this format, the hook will prevent the commit from proceeding and display an error message.

1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh

COMMIT_MSG_FILE=$1
COMMIT_MSG=`cat $COMMIT_MSG_FILE`

if [[ "$COMMIT_MSG" =~ \[ID:[[:space:]]*[0-9]+\] ]]; then
  exit 0
else
  echo "Commit message does not include a work item ID in the format [ID: <work item ID>]"
  exit 1
fi

This script is useful for enforcing commit message standards in a development workflow, especially when work items, issues, or tasks need to be tracked and linked to specific commits.

Share the goodness of Git Hooks with the team

The .git/hooks folder is not committed into source control, so to share Git hooks with your team members you need to move the hooks you want to share to another folder and remap Git hooks to that folder which can be committed into source control.

For mapping, you need to update the configuration for your Git repo like the following:

Git config --local core.hooksPath NEW-FOLDER-LOCATION

If you would like to change this configuration globally, you need to amend the --local switch to be --global

Git config --global core.hooksPath NEW-FOLDER-LOCATION

Note: You can obtain the value of the core.hooksPath configuration setting for the current Git repository by executing the following command:

git config --get core.hooksPath

Share Git Hooks

Bypass Git Hooks

To overwrite the Git hooks you have configured on the client-side, you can do so by using the no-verify switch

git commit -m "commit message" --no-verify

This post is licensed under CC BY 4.0 by the author.