A couple of companies ago, I had a report that wanted to prettify our whole codebase. His idea? Prettier in pre-commit hooks! That way, you write, commit and it’s pretty. Pretty awesome right…
Nope.
This works in a narrow use case. You write a change and commit everything you just wrote. This is, in my experience is not at all how I use git. I’ll make a bunch of changes and commit…some of them. There’s 2 reasons I do this:
I’ll never commit some changes like adding logs, custom builds, removing flaky tests
I break up my changes into lots of small commits. Often times my creation process is super messy. I don’t really know what I’m doing until it’s done. But once it’s done, then it’s clear how to break the change up into bite size commits to make a clean history that’s easier to bisect and analyze for future developers.
Prettier sadly works on not the diff but on entire repos, so this fails.
Yes I can git stash && git commit && git stash pop, but then if there’s conflicts…yuck, things get messy. Easier to run prettier manually at this point.
If you unfortunately are in this situation…use this:
git commit --no-verify
git commit -n
A tool exists which (partially) solves this problem. I find it tips the trade-offs back in favour of pre-commit formatting https://github.com/lint-staged/lint-staged