Git hooks

Git hooks are scripts that run automatically every time a particular event occurs in a Git repository (e.g. $ git commit, $ git push, etc.).

Git pre-commit example

1. Open .git/hooks dir

By default, every Git repo has a .git/hooks directory (a hidden folder).

2. Rename pre-commit.sample

Change pre-commit.sample file name to pre-commit (by removing .sample). If you'd like to enable another hook, apply the same change to it.

3. Add script to pre-commit hook


In order to run the following script properly, you will need to install jq:

Copy-past the following code into your pre-commit file:


4. Test your hook

Make a change in a Kubernetes yaml file and make a commit. The bash script will look for all the yaml files that have been committed and will run datree test [yaml file] on each of them.