Discussion of the Benefits and Drawbacks of the Git Pre-Commit Hook

11 points by domenkozar


alper

I have so far never seen pre-commit hooks that were bearable during every day development.

msfjarvis

I struggled with the same issues and ended up having my team use a pre-push hook instead and adopt git-absorb and spr, which makes it easier to handle individual commits as a unit of work when your linting runs on the full branch instead of every commit.

Rovanion

I only use a pre-push-hook that checks whether the remote is "origin" and safeguards me against causing issues for my colleagues by running our tests.

For example for a puppet repository:

$ cat .git/hooks/pre-push
#!/bin/sh
if [ "$1" = "mars" ]; then
  rake all
fi
skeptrune

Pre-commit hooks are awful. They always end up taking forever and then people turn them off. This means you can't trust that they actually ran which is worse than useless. CI should be the ultimate arbiter of truth in all scenarios.

kubanczyk

Elephant in the room is that some people are migrating to jj and it sorta deprecates this commit-centric UX.

With jj I'm very rarely on top of my branch. Mostly I've switched to mid-branch to the grand-grand-grandfather to tweak it (implicit rebase is being done for me as I go). At some point, I'll want to go another two commits down. At that time it would be very clunky UX for jj to bother me with any kind of hook for either the change that I'm just leaving, or the four changes stacked on top of it. I'm thinking about something else, dammit.

As I said the whole experience is just not centered on "done up to now, check me" as I'm more free to jump around the branch.

Only pre-push hooks make sense in this context.

ku

I don't mind git pre commit hooks if we can somehow limit them only to a limited set of branches such as master, main, development.

Other than that if I make a branch locally, I should be able to do anything there. If I want to temporarily commit "hunter2" hard coded, I should be able to do that.

arxanas

One question to ask here is: what specifically is accomplished by adding these checks to the commit step in the software development lifecycle, rather than anywhere else? The only real constraint I see is that secrets must be checked prior to pushing.

Examples:

(I personally end up doing most checks+fixes prior to pushing, to keep them out of the "inner dev loop", and to run them in parallel for each commit rather than serially.)

ubernostrum

I would be interested to see the overlap between people who think pre-commit hooks are bad because they add friction to development, and who also believe mandatory static typing is great because it catches lots of issues quickly.

For the record: I am generally in favor of pre-commit hooks to do a few basic checks, and also generally believe that it's not that hard to have an autoformatter running on file save or a linter always-on in your editor/IDE so you don't even get to the point of trying to commit things the hooks would fail on. People who complain endlessly about pre-commit slowing them down, in my experience, stop complaining about them once those sorts of "upstream" practices get improved.