Today I learned how to run the black Python code formatter as a pre-commit hook on git.

A picture of a hook from a construction crane.
Photo by Grace To on Unsplash.

Pre-commit hooks

Pre-commit hooks are scripts that git runs right before you commit something. The idea is that you will install scripts that help verify that, whatever is about to be committed, is fine.

Pre-commit hooks are just a subset of all the hooks that git can run for you. You can (and probably should) check the documentation on this subject, or you might want to check the .git/hooks directory of a git repository you have lying around. What you will notice is that the hooks directory, that was created automatically by git, contains a series of files ending with .sample that represent samples of the various hooks that you can configure.

The hook scripts, by the way, can be any executable you want. For example, they can be Python scripts with a shebang, making them executable as well.

Formatting Python code with black

If you want to make sure your Python project has its code formatted according to black's recommendatitons, you can define your pre-commit hook to run black on your code before the code is committed! This would ensure that no code is committed that isn't formatted properly.

In order to create this pre-commit hook, just go to .git/hooks and create a file called pre-commit, then put this in:

exec black . --check

That's it, that's more than enough! Of course, you can get all sorts of fancy and configure black in a non-standard way, run the code-formatting check on a specific directory, or actually tell black to format the code, instead of checking if the formatting is appropriate.

Quite cool, right?

That's it for now! Stay tuned and I'll see you around!

Become a better Python 🐍 developer πŸš€

+35 chapters. +400 pages. Hundreds of examples. Over 30,000 readers!

My book β€œPydon'ts” teaches you how to write elegant, expressive, and Pythonic code, to help you become a better developer. >>> Download it here πŸπŸš€.


Previous Post Next Post

Blog Comments powered by Disqus.