In Git, hooks are scripts that it will run for you automatically before or after certain actions.
A set of sample hook scripts are placed for your reference in the
.git/hooks directory of every Git repository. These files have a
.sample suffix and are ignored by Git. If you remove the suffix, they become active hooks that Git will invoke. The names of these scripts explain when they are run.
You can also write your own scripts, rename them appropriately and place in
.git/hooks directory. They are used by Git as soon as you place the files there.
Most of the hooks are client-side, while a few are server-side. You typically cannot run the server-side hooks since servers are typically Github or Gitlab these days.
pre-commit: This hook is executed by Git before every one of your commits. If the hook script returns a non-zero value, your commit will fail. Only if the hook script return a zero value will Git allow you to proceed and enter a commit message. This hook is a great place to run code checkers and code formatters on the changes or files being committed.
Probably the biggest irritation of hooks is that they cannot be checked into your repository, since contents of the
.git directory cannot be checked in. So you cannot maintain the hooks in version control of the repository or push the hooks to all users of the repository. This also means that the hooks cannot be enforced automatically on your team or collaborators. One (imperfect) solution is to checkin the hook scripts as normal files in the repository and have the
.git/hooks files be symlinks to the normal files. Note that users still need to create the symlinks manually:
$ ln -s ../../some/path/in/repo/pre-commit .git/hooks/pre-commit