Skip to content
Back to Journal

Supercharge your code repository with pre-commit hooks

Pre-commit workflow illustration showing automated checks before changes are committed

Several years ago, I had the opportunity to build a new platform and a new team. I was going to be leading the team and was given a modest budget, with an assurance for some more 6 months later. I managed to bring in a grad and a mid-level to join me in this quest — the quest to deliver value to the business in quick increments. Hiring a young team comes with its own set of challenges. As the leader, I had to do most of the heavy lifting that comes with building a greenfield platform. On the other hand, provide mentorship and support to your team while they contributed to the cause, with calm and patience. One of the advantages of having a young team though, is that they are raring to to learn, build, experiment and quick to course correct. They are not strongly opinionated and hence, you can shape and influence their thinking and help them grow in their career with strong foundations.

The first year was all about building custom implementations. It meant steep learning curve in software development and programming practices. As the development pace increased, so did the the number of pull requests to be reviewed and merged. Soon enough, it came to the point where entire days were spent reviewing PRs while my team waited for me to unblock them. Me, being a stickler for best practices, was at times a little too critical of teams work that led to many a back & forth before the PRs got their approval. In hindsight, I do hope they saw the value in that exercise. It could also be my inability to help them understand the importance of code quality.

Enter Pre-commit

That is when it dawned upon me that I should shift-left. I was causing my team much grief providing feedback much later in the process that would have them go back and refactor the same piece of code. This is when I thought it made sense to introduce git hooks to our codebase.

I was first introduced to the pre-commit project when I was employed at THE ICONIC. Its implementation in the codebase ensured our 20-odd members in the team ended up building and deploying code that conformed to specific guidelines that we all had agreed to. So, while one engineer may fancy tabs over spaces, and another preferred camelcase — when it came down to committing the code, Pre-commit hook kicked off to ensure all staged files conformed to the team guidelines.

Having used it for over 7 years now, I would highly recommend engineering teams adopt it. It is open source and supports custom hook implementation, while there are many community built solutions available that your team can adopt.

Setup

Install with python:

Bash
pip install pre-commit
pre-commit --version

The standard workflow is config file, which can be generated with pre-commit sample-config → pre-commit install → hooks automatically run against staged files on each commit.

Create .pre-commit-config.yaml just like one below that consists of one or more plugins:

YAML
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v6.0.0
    hooks:
      - id: trailing-whitespace
        args: [--markdown-linebreak-ext=md]
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-json
      - id: check-merge-conflict
      - id: check-added-large-files
        args: [--maxkb=1000]
      - id: mixed-line-ending
        args: [--fix=lf]

Install the git-hook with pre-commit install you should see something like:

pre-commit installed at .git/hooks/pre-commit

Run a test in your repository:

Plain text
> pre-commit run --all-files

Trim Trailing Whitespace.................Passed
Fix End of Files.........................Passed
Check Yaml...............................Passed
Check JSON...............................Passed
Check for merge conflicts................Passed
Check for added large files..............Passed
Mixed line ending........................Passed

If the hook modifies something, then review and simply:

Bash
git diff
git add .
git commit

Advanced Features I recommend

Temporarily disabling hooks

Sometimes the hooks implemented may fail due to reasons beyond your control. You can temporarily bypass by using SKIP environment variable inline on your command prompt.

Bash
$ SKIP=pytest git commit -m "commit something"

Filtering files with types and exclude

We maintained a monorepo that hosted many file types along with python and sql files. We also hosted IaC code written in python which had its unique style that required us to exclude those files from pre-commit runs. An example of how to do just that is below:

YAML
hooks:
  - id: my-hook
    files: ^src/.*\.py$
    exclude: ^infra/

The above example will include files src/test.py but exclude all files under infra directory.

Fin

Over years, I have come to find these little gems that have helped improve productivity for myself and my team. In an agentic AI era, git hooks become even more important — they are the means for bringing some much required determinism in your CICD pipelines. With hooks, you could also ensure your secrets and API keys are prevented from committed to git remote.

Additional reading: