What are Git hooks

Improve your development workflow

Stephanie Zhan
Level Up Coding

--

Photo by James Wheeler on Unsplash

Git is a popular version control system used by developers to manage their code repositories. One of the features of Git that many developers find useful is the ability to set up “hooks” that can be triggered at certain points in the development process. In this article, I’ll explore what Git hooks are, how they work, and how you can use them to improve your development workflow.

What are Git hooks?

Git hooks are scripts that can be executed automatically by Git at certain points in the development process. These scripts can be used to perform various tasks, such as running tests, checking for code quality issues, or sending notifications to team members. Git hooks are stored in a .git/hooks directory within your Git repository, and are executed by Git automatically when certain events occur.

Git provides two types of hooks: client-side and server-side. Client-side hooks are executed on the developer’s machine when a certain Git command is run, while server-side hooks are executed on the server where the Git repository is hosted. In this article, I will focus on client-side hooks, as they are the most commonly used type of hook.

How do Git hooks work?

Git hooks are triggered by certain Git events, such as committing code or pushing code to a remote repository. When one of these events occurs, Git searches for a corresponding hook script in the .git/hooks directory. If a script with the correct name is found, Git executes it automatically.

There are several Git events that can trigger a hook script. Some of the most commonly used events include:

  • pre-commit: This event is triggered before a commit is made and can be used to run tests or check for code quality issues.
  • post-commit: This event is triggered after a commit is made and can be used to send notifications or perform other tasks.
  • pre-push: This event is triggered before code is pushed to a remote repository, and can be used to run additional tests or checks.
  • post-receive: This event is triggered after code is pushed to a remote repository, and can be used to perform tasks such as deploying code to a production server.

Git hooks are written in any scripting language that can be executed on the developer’s machine, such as Bash or Python. The script should be named after the Git event it is associated with, and should be executable (chmod +x) so that Git can run it.

How can Git hooks be used?

Git hooks can automate various tasks in your development workflow, such as running tests, checking for code quality issues, or sending notifications to team members. Here are some examples of how Git hooks can be used:

Running tests

One common use case for Git hooks is to run tests before code is committed or pushed. This helps ensure that the code is in a working state before it is added to the repository. Here’s an example of a pre-commit hook that runs tests before a commit is made:

#!/bin/bash
npm test

This hook runs the npm test command before a commit is made, which in turn runs all tests in the project.

Checking for code quality issues

Another use case for Git hooks is to check for code quality issues, such as code style violations or security vulnerabilities. Here’s an example of a pre-commit hook that checks for code style issues using ESLint:

#!/bin/bash
npx eslint .

This hook runs the npx eslint . command before a commit is made, which in turn checks all JavaScript files in the project for code style issues.

Sending notifications

Git hooks can also be used to send notifications to team members when certain events occur. For example, a post-receive hook can be used to send an email or Slack message to the team when code is pushed to the remote repository. Here’s an example of a post-receive hook that sends a Slack message when code is pushed to the master branch:

#!/bin/bash

# Slack webhook URL
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/XXXXXXXXX"

# Get branch name from Git ref
read oldrev newrev refname
branch=$(git rev-parse --symbolic --abbrev-ref $refname)

# If branch is master, send Slack message
if [ "$branch" == "master" ]; then
curl -X POST --data-urlencode "payload={\"text\": \"Code has been pushed to the master branch!\"}" $SLACK_WEBHOOK_URL
fi

This hook uses a Slack webhook URL to send a message to a Slack channel when code is pushed to the master branch. The git rev-parse --symbolic --abbrev-ref $refname command is used to get the name of the branch that was pushed to.

Conclusion

By using Git hooks effectively, you can save time and improve the quality of your code.

Thanks for reading! If you are interested in articles like this, please consider supporting me by becoming a Medium member.

--

--