Skip to content

code quality

🧼 Pre-commit: Because “oops, forgot to format” is so last year

As a solo developer, I wear all the hats. πŸŽ©πŸ‘·β€β™‚οΈπŸŽ¨
That includes the very boring Quality Assurance Hatβ„’ β€” the one that says β€œyes, Amedee, you do need to check for trailing whitespace again.”

And honestly? I suck at remembering those little details. I’d rather be building cool stuff than remembering to run Black or fix a missing newline. So I let my robot friend handle it.

That friend is called pre-commit. And it’s the best personal assistant I never hired. πŸ€–


🧐 What is this thing?

Pre-commit is like a bouncer for your Git repo. Before your code gets into the club (your repo), it gets checked at the door:

β€œWhoa there β€” trailing whitespace? Not tonight.”
β€œMissing a newline at the end? Try again.”
β€œThat YAML looks sketchy, pal.”
β€œYou really just tried to commit a 200MB video file? What is this, Dropbox?”
β€œLeaking AWS keys now, are we? Security says nope.”
β€œCommit message says β€˜fix’? That’s not a message, that’s a shrug.”

Pre-commit runs a bunch of little scripts called hooks to catch this stuff. You choose which ones to use β€” it’s modular, like Lego for grown-up devs. 🧱

When I commit, the hooks run. If they don’t like what they see, the commit gets bounced.
No exceptions. No drama. Just β€œfix it and try again.”

Is it annoying? Yeah, sometimes.
But has it saved my butt from pushing broken or embarrassing code? Way too many times.


🎯 Why I bother (as a hobby dev)

I don’t have teammates yelling at me in code reviews. I am the teammate.
And future-me is very forgetful. πŸ§“

Pre-commit helps me:

  • πŸ“ Keep my code consistent
  • πŸ’£ It catches dumb mistakes before I make them permanent.
  • πŸ•’ Spend less time cleaning up
  • πŸ’Ό Feel a little more β€œpro” even when I’m hacking on toy projects
  • 🧬 It works with any language. Even Bash, if you’re that kind of person.

Also, it feels kinda magical when it auto-fixes stuff and the commit just… works.


πŸ›  Installing it with pipx (because I’m not a barbarian)

I’m not a fan of polluting my Python environment, so I use pipx to keep things tidy. It installs CLI tools globally, but keeps them isolated.
If you don’t have pipx yet:

python3 -m pip install --user pipx
pipx ensurepath

Then install pre-commit like a boss:

pipx install pre-commit

Boom. It’s installed system-wide without polluting your precious virtualenvs. Chef’s kiss. πŸ‘¨β€πŸ³πŸ’‹


πŸ“ Setting it up

Inside my project (usually some weird half-finished script I’ll obsess over for 3 days and then forget for 3 months), I create a file called .pre-commit-config.yaml.

Here’s what mine usually looks like:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v5.0.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-added-large-files

  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.28.0
    hooks:
      - id: gitleaks

  - repo: https://github.com/jorisroovers/gitlint
    rev: v0.19.1
    hooks:
      - id: gitlint

  - repo: https://gitlab.com/vojko.pribudic.foss/pre-commit-update
    rev: v0.8.0
    hooks:
      - id: pre-commit-update

πŸ§™β€β™‚οΈ What this pre-commit config actually does

You’re not just tossing some YAML in your repo and calling it a day. This thing pulls together a full-on code hygiene crew β€” the kind that shows up uninvited, scrubs your mess, locks up your secrets, and judges your commit messages like it’s their job. Because it is.

πŸ“¦ pre-commit-hooks (v5.0.0)

These are the basics β€” the unglamorous chores that keep your repo from turning into a dumpster fire. Think lint roller, vacuum, and passive-aggressive IKEA manual rolled into one.

  • trailing-whitespace:
    🚫 No more forgotten spaces at the end of lines. The silent killers of clean diffs.
  • end-of-file-fixer:
    πŸ‘¨β€βš•οΈ Adds a newline at the end of each file. Why? Because some tools (and nerds) get cranky if it’s missing.
  • check-yaml:
    πŸ§ͺ Validates your YAML syntax. No more β€œwhy isn’t my config working?” only to discover you had an extra space somewhere.
  • check-added-large-files:
    🚨 Stops you from accidentally committing that 500MB cat video or .sqlite dump. Saves your repo. Saves your dignity.
πŸ” gitleaks (v8.28.0)

Scans your code for secrets β€” API keys, passwords, tokens you really shouldn’t be committing.
Because we’ve all accidentally pushed our .env file at some point. (Don’t lie.)

✍️ gitlint (v0.19.1)

Enforces good commit message style β€” like limiting subject line length, capitalizing properly, and avoiding messages like β€œasdf”.
Great if you’re trying to look like a serious dev, even when you’re mostly committing bugfixes at 2AM.

πŸ” pre-commit-update (v0.8.0)

The responsible adult in the room. Automatically bumps your hook versions to the latest stable ones. No more living on ancient plugin versions.

🧼 In summary

This setup covers:

  • βœ… Basic file hygiene (whitespace, newlines, YAML, large files)
  • πŸ”’ Secret detection
  • βœ‰οΈ Commit message quality
  • πŸ†™ Keeping your hooks fresh

You can add more later, like linters specific for your language of choice β€” think of this as your “minimum viable cleanliness.”

🧩 What else can it do?

There are hundreds of hooks. Some I’ve used, some I’ve just admired from afar:

  • black is a Python code formatter that says: β€œShhh, I know better.”
  • flake8 finds bugs, smells, and style issues in Python.
  • isort sorts your imports so you don’t have to.
  • eslint for all you JavaScript kids.
  • shellcheck for Bash scripts.
  • … or write your own custom one-liner hook!

You can browse tons of them at: https://pre-commit.com/hooks.html


πŸ§™β€β™€οΈ Make Git do your bidding

To hook it all into Git:

pre-commit install

Now every time you commit, your code gets a spa treatment before it enters version control. πŸ’…

Wanna retroactively clean up the whole repo? Go ahead:

pre-commit run --all-files

You’ll feel better. I promise.


🎯 TL;DR

Pre-commit is a must-have.
It’s like brushing your teeth before a date: it’s fast, polite, and avoids awkward moments later. πŸͺ₯πŸ’‹
If you haven’t tried it yet: do it. Your future self (and your Git history, and your date) will thank you. πŸ™

Use pipx to install it globally.
Add a .pre-commit-config.yaml.
Install the Git hook.
Enjoy cleaner commits, fewer review comments β€” and a commit history you’re not embarrassed to bring home to your parents. πŸ˜ŒπŸ’

And if it ever annoys you too much?
You can always disable it… like cancelling the date but still showing up in their Instagram story. πŸ˜ˆπŸ’”

git commit --no-verify

Want help writing your first config? Or customizing it for Python, Bash, JavaScript, Kotlin, or your one-man-band side project? I’ve been there. Ask away!