How to Use Git Hooks?

Developing a quality application is a complex process. To do it, we use tools that complement the coding process. One of these processes is automation. This is due to the need for developers and sysadmins to reduce deployment time. Git is one of the most popular tools for this specific purpose. That’s why in this tutorial, we’ll show you how to use Git hooks to enhance your VPS project!

Git hooks are an internal utility that will improve the way Git is used. Let’s learn exactly how they work!

What is Git?

Before tackling the subject of Hooks, let’s quickly cover what Git is.

Git is an open source version control application. With it, we can monitor the development of software in detail.

Git is very popular among developers, and most open source projects use it.

What are Git Hooks

With Git we will be able to create branches of development, register changes and have absolute control over the versions. However, it is possible to automate this process. Git automation works at the level of programs and deployment. That is what Hooks are for.

Git hooks are shell scripts that run automatically before or after Git executes an important command like Commit or Push. For a Hook to work, it is necessary to give the Unix system the execution permissions.  By using these scripts, we can automate certain things.

Git includes examples of Git hooks as soon as a local repository is started. It should be noted that sites like Github or Gitlab do not allow their use in the first instance. That is why they are mainly used in local or private instances.

How to Use Git Hooks

Hooks are stored in the .git/hooks/ folder of each cloned project, or in a newly created local repository. There we will find a series of example Hooks, which are a good starting point.

To activate a hook, it is only necessary to create a file and save it in the .git/hooks/ folder. However, the name of the file is predefined by Git.  Some of the Hooks we can use are:

Git Hook Git Command Description
applypatch-msg.sample git am When a patch message is changed
commit-msg.sample git commit To set the message of a commit action
fsmonitor-watchman.sample launch watchman To integrate watchman
post-update.sample git push By updating all data after the push
pre-applypatch.sample git am Before applying a patch
pre-commit.sample git commit Before committing
prepare-commit-msg.sample git commit When a commit message is set
pre-push.sample git push Before making a push
pre-rebase.sample git rebase Before making a pass or merge
pre-receive.sample git push When we push and get the data from the remote repository
update.sample git push By updating the remote data in a push

As we can see, every Hook carries a command in Git. With that, we can take it into account when it is convenient to make a Hook.

Git Hook Examples

The use of Hooks is very wide, but knowledge of Bash and other languages such as Python or Ruby are needed to use their full potential. Here are some basic examples:

Showing Information About a Commit Action

This example shows the information of a commit action. Make a file called prepare-commit-msg in the .git/hooks/ folder of our repository. Then, write the following script:

#!/bin/sh

SOB=$(git config github.user)
grep -qs "^$SOB" "$1" || echo ". Cambio por @$SOB" >> "$1"

Then, save it. And set the execution permissions to the file.

:~$ chmod +x prepare-commit-msg

With this simple Hook, when a commit action is made, we’ll instantly receive relevant information about it.

Generate Documentation as Changes are Uploaded

This pre-push Hook allows generating the documentation of our code if we have a generator. Every time we make a change, the documentation will be performed automatically.

Create pre-push in the same folder as before and add the script:

#!/bin/bash

doxygen Doxyfile
git add docs/
git commit -m "Update documentation ($(date +%F@%R))"

Save the file, and finally, set the execution permission.

:~$ chmod +x pre-push

Find and Fix Trailing Whitespace in Commits

This pre-commit type Git hook is quite simple to make. Make a file called pre-commit and add the following:

#!/bin/bash -l

.git/hooks/pre-commit-master-no-no
if [[ $? == 1 ]]
then
exit 1
fi

.git/hooks/pre-commit-debugger
.git/hooks/pre-commit-trailing-spaces
.git/hooks/pre-commit-images
.git/hooks/pre-commit-pair

Now trailing whitespace will be found and fixed in all commits.

Conclusion

Git is a vital tool for today’s developers. Git hooks are a great way to improve it even further! In this tutorial, we reviewed the fundamental concept of automating Git processes with hooks.

Making a script for Git Hooks can be complicated if you don’t have a solid base of programming language knowledge, like bash.

Luckily here you have taken the first step towards learning how to use Git Hooks. We recommend to follow the official documentation and continue the learning process!

Author
The author

Edward S.

Edward is an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward's goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.