How to make a git pre-commit code check?

Yes, you can use a pre-commit hook.

Just drop a shell script named pre-commit (with no extension) inside your ".git/hooks" folder with the logic to check your variable and either:

  • change it to false and continue with the commit or
  • print a message telling the user to correct the value manually, and exit with a non-zero code to abort the commit

The hooks folder should contain a few samples, such as "pre-commit.sample", which you might find helpful.

From the docs:

The pre-commit hook is run first, before you even type in a commit message. It’s used to inspect the snapshot that’s about to be committed, to see if you’ve forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code. Exiting non-zero from this hook aborts the commit, although you can bypass it with git commit --no-verify. You can do things like check for code style (run lint or something equivalent), check for trailing whitespace (the default hook does exactly this), or check for appropriate documentation on new methods.


What about the following link?

http://wadmiraal.net/lore/2014/07/14/how-git-hooks-made-me-a-better-and-more-lovable-developer/

I think you can just add some regular expression to detect localMode = true into the sample code at the link.