Skip to main content

Git Hooks

Git Hooks are scripts that Git executes before or after events such as commit, push, and receive. They are a powerful feature that can automate various tasks in your Git workflow, making your projects more streamlined and consistent.

Understanding Git Hooks

Each Git repository has a '.git/hooks' directory with some sample scripts. These scripts are activated when specific events occur in the repository. For instance, a pre-commit hook runs before your commit is finalized. It's a perfect place to run tests or check for code styling issues.

Hooks are stored locally, and by default, they are not copied when you clone a repository. They're meant to be per-repository settings.

Types of Git Hooks

There are two types of hooks: client-side and server-side.

Client-side hooks are triggered by operations such as committing and merging, while server-side hooks run on network operations like receiving pushed commits.

Here are a few commonly used hooks:

  • Pre-commit: This hook runs before you even type in a commit message. It’s used to inspect the snapshot that’s about to be committed.
  • Pre-push: This hook runs during git push, before the changes are actually pushed.
  • Post-receive: This hook runs on the remote repository once the changes have been received. It’s used to update other services or notify users.

Implementing Common Git Hooks

Git Hooks can automate numerous tasks in your Git workflow, making your projects more streamlined. This tutorial will cover how to implement a few common Git Hooks.

Implementing a Pre-commit Hook

A pre-commit hook runs before a commit is made. It's typically used for tasks such as running tests or checking code style. Here's a simple example of a pre-commit hook that checks for any TODO comments left in the code:

#!/bin/sh
FILES_PATTERN='\.(js|jsx)$'
FORBIDDEN='TODO'
git diff --cached --name-only | \
grep -E $FILES_PATTERN | \
GREP_COLOR='4;5;37;41' xargs grep --color --with-filename -n $FORBIDDEN && echo 'COMMIT REJECTED Found "$FORBIDDEN" references. Please remove them before committing.' && exit 1
exit 0

To use this script, place it in the .git/hooks directory of your repository and name it pre-commit.

Implementing a Pre-push Hook

A pre-push hook runs before changes are pushed to the remote repository. Here's an example of a pre-push hook that prevents master and develop branches from being pushed directly:

#!/bin/sh
protected_branch='master develop'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

if [ $protected_branch = $current_branch ]
then
echo "Direct push to $current_branch is not allowed. Please create a pull request."
exit 1
fi
exit 0

Like the pre-commit hook, this script should be placed in the .git/hooks directory and named pre-push.

Conclusion

Git Hooks can be a powerful tool to automate your Git workflows and enforce rules at various stages of development. By understanding what Git Hooks are, you can start to see how they could be used to improve your projects.