๐งผ 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.sqlitedump. 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:
blackis a Python code formatter that says: โShhh, I know better.โflake8finds bugs, smells, and style issues in Python.isortsorts your imports so you donโt have to.eslintfor all you JavaScript kids.shellcheckfor 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!


