Over a million developers have joined DZone.

Utility of the Pre-Commit Hook

DZone's Guide to

Utility of the Pre-Commit Hook

Learn about pre-commit hooks and linters and how they let you do a baseline check before you commit and push your code to the repository.

· DevOps Zone ·
Free Resource

Learn how integrating security into DevOps to deliver "DevSecOps" requires changing mindsets, processes and technology.

Cook your application code and use the capabilities of “pre-commit linters” to do a baseline check before you commit your code and push it to the repository. So, I assume my readers would have the following questions:

  • What is a “hook”?
  • What is “Pre-commit”?
  • What is the problem statement that pre-commit resolves?
  • What are “Pre-commit hooks”?
  • What is a “linter”?

What Is a “Hook”?

In programming, a hook is an application, a set of customized program code that helps to perform another functionality. This simplifies interoperability between two applications or platforms. In technical terms, it is a callback for an event or set of events triggered by certain actions, which might be clicking a button or running certain specific scripts.

What Is “Pre-Commit”?

Pre-commit is a framework for managing and maintaining multi-language pre-commit hooks.

What Is the Problem Statement That Pre-Commit Resolves?

Git hook scripts are very useful for identifying simple issues before submission to code review. We run these hooks on every commit to automatically point out issues in the code, such as missing semicolons, trailing whitespace, and debug statements. Pointing out these issues before code review allows a code reviewer to focus more on the architecture of the application rather than petty issues.

As we keep on creating more libraries and projects, it becomes complicated to share pre-commit hooks across projects. It is recommended that we always use the best industry standard linters.

What Are “Pre-Commit Hooks”?

The pre-commit platform solves the hook issues. It is a multi-language package manager for pre-commit hooks. We specify a list of hooks we want and pre-commit manages the installation and execution of any hook written in any language before every commit. Pre-commit is specifically designed to not require root access. Pre-commit automatically handles downloading and building from packages without root.

What Is a “Linter”?

A “linter” or “lint” is a tool or certain program/programs that analyze the source code to flag programming errors, bugs, style-based errors, and suspicious constructs. The term originated from a UNIX utility that examined C language source code.


Installing pre-commit can be done in two ways:

Using pip:

pip install pre-commit

Using Home-Brew for MacOS:

brew install pre-commit

For system level installation:

curl https://bootstrap.pypa.io/get-pip.py | sudo python - pre-commit

In a Python-based project:

In a Python-based project, you can add the dependency to the requirements.txt

Add 'pre-commit' to the requirements.txt
Run pip install -r requirements.txt

Adding Pre-Commit Plugins to Your Project

Once you have pre-commit installed, adding pre-commit plugins to your project is done with the .pre-commit-config.yaml configuration file.

Add a file called .pre-commit-config.yaml to the root of your project. The pre-commit config file describes what repositories and hooks are installed.

A typical .pre-commit-config.yaml would look something like this:

- repo: git://github.com/pre-commit/mirrors-yapf
  sha: v0.20.2
  - id: yapf
  - repo: https://github.com/pre-commit/pre-commit-hooks
    sha: v1.2.0
  - id: trailing-whitespace
  - id: check-merge-conflict

In the above example, I have included plugins for the Python linter, which is yapf; “trailing-whitespace” to remove trailing whitespace and “check-merge-conflict” to check for merge conflicts in the file.

You would also have to add a “.style.yapf” file, which would have the style standards defined within. The “.style.yapf” file would look something like this:

based_on_style = pep8

Updating Hooks Automatically

You can update your hooks to the latest version automatically by running pre-commit autoupdate. This will bring the hooks to the latest sha on the master branch.


You have to run pre-commit install to install pre-commit into your git hooks. pre-commit will now run on every commit. Every time you clone a project using pre-commit running pre-commit install --install-hooks should always be the first thing you do. Check the screenshot for reference.

Screen Shot 2018-02-19 at 4.24.22 PM

If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files. To run individual hooks, use pre-commit run .

The first time pre-commit runs on a file, it will automatically download, install, and run the hook. Note that running a hook for the first time may be slow. For example: if the machine does not have Node installed, pre-commit will download and build a copy of Node.

To get started with the pre-commit checks, just commit your code to see whether your codebase follows industry standards or not. Check the screenshot for reference.

Screen Shot 2018-02-19 at 4.33.35 PM.png

Updating Hooks Automatically

You can update your hooks to the latest version automatically by running pre-commit autoupdate. This will bring the hooks to the latest sha on the master branch. Check the screenshot for reference.

Screen Shot 2018-02-19 at 4.36.48 PM.png

Learn how enterprises are using tools to automate security in their DevOps toolchain with these DevSecOps Reference Architectures.

github ,hooks ,devops

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}