How to use Git Hooks?

Developing a quality app is a complex process. To do so, we use tools that complement the coding process. One of these coding processes is automation. This is due to the need for developers and system administrators to reduce deployment time. it 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 project!

Git Hooks are an internal utility that will improve the way you use Git. Let’s learn exactly how they work!

What is Git?

Before we get into the topic of Hooks, let’s quickly take a look at what Git is.

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

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

What are Git Hooks?

With Git we can create development branches, register changes and have absolute control over versions. However, it is possible to automate this process. Git automation works at the program and deployment level. And for that there are Hooks.

Git hooks are shell scripts that are automatically executed before or after Git executes an important command like commit or push. For a Hook to work, it is necessary to grant the Unix system execute permissions. By using these scripts, we can automate certain things.

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

See also  What is localhost?

How to use Git Hooks

Hooks are stored in the folder .git/hooks/ 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 folder .git/hooks/. However, the filename will be predefined by Git. Some of the hooks that we can use are:

git-hookgit commandDescriptionapplypatch-msg.samplegit amWhen a patch message is changedcommit-msg.samplegit commitTo configure the message of a commit actionfsmonitor-watchman.samplelaunch watchmanTo integrate watchmanpost-update.samplegit pushTo update all data after the pushpre-applypatch.samplegit amBefore apply a patchpre-commit.samplegit commitBefore doing a commitprepare-commit-msg.samplegit commitWhen a commit message is setpre-push.samplegit pushBefore doing a pushpre-rebase.samplegit rebaseBefore doing a pass or mergepre-receive.samplegit pushWhen we push and we get the data from the remote repository update.samplegit pushTo update the remote data in a push

As we can see, each hook is associated with a Git command. In this way we can know when it is convenient to use a hook.

Examples of how to use Git Hooks

Hooks are widely used, but knowledge of Bash and other languages ​​such as Python or Ruby are needed to take advantage of their full potential. Even so, here are some basic examples of its use:

Show information about a commit action

This example displays the information for a commit action. Create a file called prepare-commit-msg in the folder .git/hooks/ from your repository. Then write the following script:

#!/bin/sh SOB=$(git config github.user) grep -qs “^$SOB” “$1” || echo “. Change to @$SOB” >> “$1”

Then save it. And set the execute permissions for the file.

See also  What is cPanel? Pros and cons + how to use it

:~$ chmod +x prepare-commit-msg

With this simple Hook, when performing a commit action, you will instantly receive relevant information about it.

Generate documentation as changes are uploaded

This pre-push Hook allows you to generate the documentation of your code if you have a generator. Every time you make a change, the documentation will run automatically.

Create a 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 execute permission.

:~$ chmod +x pre-push

Find and fix whitespace in commits

This pre-commit type Git Hook is quite simple to run. Create a file called pre-commit and add the following:

#!/bin/bash -l .git/hooks/pre-commit-master-no-no if ]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

Whitespace in all commits will now be found and corrected.

conclusion

Git is a vital tool for developers today. Git Hooks are a great way to make it even better! In this tutorial, we review the fundamental concept of automating processes with Git Hooks.

Scripting for Git Hooks can be tricky if you don’t have a solid foundation of knowledge in programming languages, like bash.

Fortunately, with this tutorial you have taken the first step in learning how to use Git Hooks. We recommend you follow the official documentation and continue the learning process!

Deyi is a digital marketing enthusiast, with a background in web design, content creation, copywriting, and SEO. She is part of ‘s SEO & Localization team. In her free time, she likes to develop projects, read a book or watch a good movie.

Loading Facebook Comments ...
Loading Disqus Comments ...