Git hooks allow you to run custom scripts whenever certain important events occur in the Git life-cycle, such as committing, merging and pushing.
— tygertec, Youtuber
Hooks are found in .git/hooks and to run them
You have to remove .sample extension
Make them executable chmod +x hook_name
Hooks(.git/hooks) are not pushed to source control(git / bitbucket)
Hooks can be bypassed by using --no-verify flag
Scripting languages can be anything available in the system like #!/bin/sh or #!/bin/bash or python or perl anything.
Exit return codes have to be 0(success) / 1(failure)
There are many hooks and here we are going to see two of them
pre-commit - This hook is invoked git commit. It takes no parameters, and is invoked before obtaining the proposed commit log message and making a commit. Exiting with a non-zero status from this script causes the git commit command to abort before creating a commit.
prepare-commit-msg - This hook is invoked by git-commit right after preparing the default log message, and before the editor is started. It takes one to three parameters. The first is the name of the file that contains the commit log message. The second is the source of the commit message, and can be: message or template or squash or commit. If the exit status is non-zero, git commit will abort. The purpose of the hook is to edit the message file in place, and it is not suppressed by the —no-verify option. A non-zero exit means a failure of the hook and aborts the commit. It should not be used as replacement for pre-commit hook.
pre-commit
This hook does 3 things
Checks if the filenames are ASCII Complaint
File names should start either with I or B
Files should be uppercase
prepare-commit-msg
This hook checks whether the commit message starts with ABCD=
This hook just sort of prepends commit message with month and year
Initially wanted to try pre-receive hook. This hook executes once for the receive operation and thought i can club some of above operations/checks at the server side. But it seems, pre-receive hook is available at only Git Enterprise. Anyhow it was interesting to learn.