Git & GitHub - Git Hooks
Introduction to Git hooks and their usage
Git hooks are scripts that are executed at specific points in the Git lifecycle. They allow you to automate tasks, enforce policies, and enhance your workflow. This guide introduces Git hooks and explains how to use them effectively.
Key Points:
- Git hooks are scripts that run automatically at certain points in the Git workflow.
- Hooks can be used to automate tasks, enforce coding standards, and integrate with other tools.
- There are two types of hooks: client-side and server-side.
Types of Git Hooks
Git hooks are classified into two main types:
Client-Side Hooks
These hooks are executed on the client machine and can be used to enforce code quality, format code, run tests, and more. Examples include pre-commit
, commit-msg
, and post-commit
.
Server-Side Hooks
These hooks are executed on the server hosting the Git repository. They are often used to enforce policies and integrate with continuous integration/continuous deployment (CI/CD) systems. Examples include pre-receive
, update
, and post-receive
.
Setting Up Git Hooks
Git hooks are stored in the .git/hooks
directory of your repository. To set up a hook, create a script file in this directory and make it executable.
# Navigate to the hooks directory
$ cd .git/hooks
# Create a new hook script
$ touch pre-commit
# Make the script executable
$ chmod +x pre-commit
Example: Pre-Commit Hook
A pre-commit hook runs before a commit is created. You can use it to run tests, lint code, or check for formatting issues. Here is an example pre-commit hook script that checks for TODO comments:
#!/bin/sh
# Example pre-commit hook that checks for TODO comments
if grep -rnw 'TODO' .; then
echo "Please resolve all TODO comments before committing."
exit 1
fi
exit 0
Example: Commit Message Hook
A commit-msg hook runs after a commit message has been entered but before the commit is finalized. You can use it to enforce commit message standards. Here is an example commit-msg hook script that enforces a specific format:
#!/bin/sh
# Example commit-msg hook that enforces a specific commit message format
commit_msg=$(cat $1)
if ! echo "$commit_msg" | grep -qE "^[A-Z][A-Za-z0-9 ]+: .{10,}$"; then
echo "Commit message must start with a capitalized subject and be at least 10 characters long."
exit 1
fi
exit 0
Sharing Git Hooks
By default, Git hooks are not shared with other users of the repository. To share hooks, you can include them in your repository and provide instructions for installation, or use tools like Githooks to manage and share hooks more easily.
# Example of adding hooks to the repository
$ mkdir -p .githooks
$ cp .git/hooks/pre-commit .githooks/pre-commit
# Instructions for installing the hooks
$ echo "Copy the hooks from .githooks to .git/hooks and make them executable." > INSTALL_HOOKS.md
Summary
This guide provided an introduction to Git hooks, including their types, setup, and examples of usage. By leveraging Git hooks, you can automate tasks, enforce policies, and integrate with other tools, enhancing your overall workflow.