Local hooks, also known as “client-side” hooks, run on your local repository. This means you can customize these local scripts for your environment without affecting any other contributors out in the web.
Most common, you will see a “pre-commit” hook utilized on the local repository. This script will run before you are even prompted to provide a commit message. The pre-commit hook, ideal for testing, allows you to run scripts on your current commit, possibly saving you from committing errors to the repository.
Once you have initialized a git repository locally, you can take a look at your hook samples by opening the hooks
directory.
From the repository working directory, type:
cd .git/hooks
Or, if you prefer to use the file manager in your operating system, make sure that the “hidden files” are visible, then open the .git
directory. You will see the hooks
directory sitting right there waiting for you.
Notice that all the scripts listed here end with a .sample
extension. In order to make these scripts executable you must remove this sample extension and leave the hook as is.
On the command line, you can use the mv
command to rename these files. For example, using the pre-commit.sample
file:
mv pre-commit.sample pre-commit
Now if you try to run a commit, you will receive a code review. Maybe your code will not trigger any warnings, but if so you can start to see how powerful these hooks can be.