With git hooks you can automate tasks when working with git

8/2022 3 minutes

Let git hooks take care of automating tasks, checking commits for errors, and much more automatically when working with a git repository.

it hooks are a series of scripts that are created automatically when you create a git repository and that are automatically “fired” on certain events when working with git. For example when creating a commit, when receiving a push, etc.

We can activate and customize them to our needs and use them to prevent errors such as sending a commit to the wrong branch, sending a notification before committing, etc.

Let’s take a look at those git hooks . As I always say, I am not an expert and that is why I always refer to the official documentation . Here we will see some example of using these git hooks.

I recommend you open a terminal on your computer and follow the steps. Let’s make a folder called test, go into it and initialize it as a git repository. All this like this:

mkdir test
cd test
git init

Every time you create a new git repository on your computer like what we have done in these steps, a hidden folder called .git will be created and inside this other folders and there is one called hooks and inside that folder several scripts ready to be able to use them, let’s see how.

Remember that this .git folder will not be pushed to the remote git repository . So they will be on your computer, but will not be part of the upstream repository.

Inside our test folder, we enter the .git/hooks folder and we will see that there are files similar to these:

applypatch-msg.sample
commit-msg.sample
fsmonitor-watchman.sample
post-update.sample
pre-applypatch.sample
pre-commit.sample
pre-merge-commit.sample
pre-push.sample
pre-rebase.sample
pre-receive.sample
prepare-commit-msg.sample
push-to-checkout.sample
update.sample
  1. pre-commit → runs before a commit is performed
  2. prepare-commit-msg → executed before writing a commit message
  3. commit-msg → executed after writing a commit message
  4. post-commit → runs after commit
  5. pre-rebase → before performing a rebase
  6. post-checkout → after performing a checkout
  7. post-merge → after performing a merge
  8. pre-receive → before receiving a push
  9. post-receive → after receiving a push
  10. update → before receiving a push, it is executed once per branch

But there is one more. These scripts are examples of how to use them as git hooks , their names are pretty indicative of when each script is executed. You can see the code content of one of them.

Some are intended to be executed from the client side and others are specific to the server side where the git repository is located.

To use them, just remove the .sample extension and give them execute rights . The script codes are a guide that we can use. But we can modify them to our needs or use another programming language like Python instead of bash if we prefer, to adapt the scripts to our needs.

Let’s enable the commit-msg script. To do this, as I said, we rename it and make it executable:

mv commit-msg.sample commit-msg 
chmod +x commit-msg 

We edit the file and we can for example paste this small script in Bash:


# Check that the commit message is greater than 4 characters and less than 30
commit=$(head -n1 $1)
 
if [[ $(echo $commit | wc -c) -lt 5 || $(echo $commit | wc -c) -gt 30 ]];then
    echo -e "The commit text must be greater than 5 characters and less than 30.\nPlease rewrite a new commit"
    exit 1
fi

With this script, we “force” whoever wants to create a commit to enter a comment that is greater than 4 characters and less than 30. If this is not fulfilled, it will show the text on the screen and the commit will not be accepted.

Like this script, we can also create another one, to check that the commits comply with certain rules, for example that they start in a certain way, or whatever we need.

Other tasks that we can enable with other scripts is to send a notification email to those responsible for the repository that a new push has been received.

Or perhaps when receiving a new contribution to our repository, execute a certain script that reflects those changes in a web page, etc…

Here it depends on the use that you want to give them for each specific case. On the net there are several examples of use of different cases for you to take a look at, in case one can be adapted to your needs.

Links of interest

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top