Installing Git pre-commit globally.

While there are a number of instructions about installing pre-commit globally on the web, I didn’t find one with all the extras to convince my colleagues. This is that:

tl;dr: a tip to minimize the hassles of using pre-commit – pre-commit is an optional tool (i.e. you can skip this, but you “shouldn’t”)

More and more of our repos are using “pre-commit” a tool that makes it easy to run a standard set of pre-commit hooks locally. No more Python syntax errors, etc. The gotcha is that you have to remember to “pre-commit install” every time you create or clone a repository. (This isn’t for python only – there are pre-written hooks for ‘go’ (examples), file formats, etc - see current list.)

Here’s how to set up your dev environment so “pre-commit” will automatically be installed in every freshly cloned repo on your dev machine. (No worries - it has no effect if there isn’t a “.pre-commit.yaml” file in the working directory – beyond an extra 1 line message[1].)

  1. Install “pre-commit” in it’s own venv, using pipx:

    pipx install pre-commit

    If you already had pre-commit installed, make sure you’ve upgraded to at least version 1.18

  2. Follow the instructions for global installation at the pre-commit site. (This utilizes the template directory feature of git, so adjust if you were already using that feature.)

You can now run “git init[2] in any existing repository to ensure pre-commit is ready to be used on any repository with a .pre-commit.yaml file.

Footnotes:

[1].pre-commit-config.yaml config file not found. Skipping pre-commit.”
[2]

from the git book:

Running git init in an existing repository is safe. It will not overwrite things that are already there.