Skip to content →

Git pre-commit hooks


Continuous integration (CI) workflows enable us to streamline the process of integrating changes and catching errors. But what if we could bring some of the power of CI to our local development environment in addition to the continuous integration server? This is where the concept of git hook scripts comes into play. In this blog post, we will be discussing the pre-commit hook project, an initiative aimed at easing the git hook script management and enabling a kind of local CI workflow to enhance our development experience and get an even shorter developer feedback loop.

Git hook scripts

Git hook scripts are scripts that automatically run at specific points in the git workflow. These scripts can automate tasks and checks before such actions as a commit or a push. They can be used to enforce code style, check for syntax errors, and perform other checks.

The Pre-commit Hook Project

The pre-commit hook project is a kind of package manager for pre-commit hooks. No more copying, pasting, and managing your scripts across all your repos. Now, suddenly, you get a repository of pre-made scripts and can upload your own for others to use. One source of pre-commit scripts is the pre-commit github repo.

Setting Up the Pre-commit Hook Project

To get started with the Pre-commit Hook Project, you'll need to have a version control system like Git installed on your local machine. Once that's sorted, you can install the pre-commit package using pip, a package manager for Python.

pip install pre-commit

After installing the package, you'll need to create a .pre-commit-config.yaml file in the root directory of your project. This file will contain the configuration for the pre-commit hooks you want to use.

Here's a simple example of how a .pre-commit-config.yaml file might look:

- repo: <>
  rev: v1.50.0 # Use the latest release version from the repo.
  - id: terraform_fmt
    files: .*\\.tf$
  - id: terraform_validate
    files: .*\\.tf$

In this example, two hooks are being used: terraform fmt, terraform_validate. These hooks will format and validate the terraform code in the terraform folder. For an example, take a look at

To activate the pre-commit hooks and install them in our repository, you'll need to run the following command:

pre-commit install

Now, every time you attempt to make a Git commit, the pre-commit hooks you've configured will automatically run.

For example, making an indent error results in the following:

In this case, the terraform fmt corrects the formatting error but leaves the change unstaged. The pre-commit hooks only run on staged files, which enables us to go through any changes before staging and committing them. It would have been pretty bad if the pre-commit hooks made changes and committed them without us validating them first.

Word of caution

While the Pre-commit Hook Project offers many benefits, it's important to note that it is not intended to replace a continuous integration server and workflow. It is designed to complement these systems, helping to catch issues early in the development process.


Git hook scripts, and specifically the Pre-commit Hook Project, offer a compelling solution for enhancing our workflows and catching errors early. By automating tasks and checks, we can ensure consistency and quality in our code before it even leaves our local environment. A good continuous integration workflow is still necessary, but with pre-commits, we can reduce the number of failed builds.

Published in productivity programming tips&tricks

Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x