Thursday 15 October 2020

How to configure git hooks: a practical example




 

Hook scripts are useful for identifying simple issues before submission to code review or committing.

Imagine you want to run some tests before committing your changes locally.

You can use a pre-commit script, thanks to git hooks!

Let's see a practical example.


Example

This a simple project in which we have initialized a git repository.

If you list the content of the root directory of the project you can see a .git directory, like this:


The .git directory structure is the following:


Inside
hooks directory, you can find all the sample scripts you can configure and use before the related git action takes effect.

In our example, we want to run a pre-commit script so that git commit can't be executed if all the conditions specified in that are not satisfied.

pre-commit.sample contains just a sample, you can substitute it with your
own code. Rename it as pre-commit to enable it.


For our example we simply do this:



Let's do a commit:


Our hook has been started and ended successfully :D

Now we can push :P



No comments:

Post a Comment