How to prevent git commit --no-verify command?

37,979

First off: you can't definitively prevent someone from passing the --no-verify option. That said, it's a good practice to use pre-commit hooks for linting, and it's a good practice to avoid passing the --no-verify option without reason.

If, however, you want to make it more cumbersome to pass the --no-verify option, you could:

  1. generate a verification token and append it to the commit message in pre-commit;
  2. exit pre-receive with a non-zero exitcode if this token is missing or invalid. (Examples of things you can do in pre-receive hooks: https://github.com/github/platform-samples/tree/master/pre-receive-hooks)

Someone determined to avoid passing --no-verify could manually do step 1, which is why this isn't 100% effective. I wouldn't recommend setting this up in a professional context, but I'm all for people using the tools at their disposal to instill good habits for themselves, while learning more about git hooks.

Share:
37,979
albert
Author by

albert

Updated on July 09, 2022

Comments

  • albert
    albert almost 2 years

    I would like to setup a pre-commit hook for all git repos to validate syntax errors using jshint and phplint. But the issue is that the git has a feature which can skip pre-commit hook from happening by using --no-verify flag. But i don't need to use that option. Can i prevent that --no-verify flag for git Please suggest a way.

  • timotgl
    timotgl about 5 years
    You could run the code that gets the verification token and use it on your own commits that didn't use the hook. It does not really prevent skipping the hook, also trying to prevent skipping the hooks is bad practice and bad organization culture. Don't babysit people with this controlling, condescending practice.
  • candu
    candu about 5 years
    @timotgl well, sure; all you can do is make it arbitrarily annoying to avoid the hook. It's a good point, though, and I've edited my answer to make this clearer. FWIW, I agree that we shouldn't police developers but strongly disagree that linting should be left until merge / CI, especially when working in teams. Code style guides enforced as close to code editing as possible - or, better yet, automated using tools like Prettier - are less time-consuming and more usable, and therefore more likely to be respected.
  • neaumusic
    neaumusic almost 5 years
    "You shouldn't be able to save your code unless you run my middleware" what a great idea
  • Wade Williams
    Wade Williams about 3 years
    This is an excellent answer to a problem that should never be solved.