What Are Git Hooks? How Do You Do a Pre-commit Hook?

24 Oct, 2023
Mutiullah Samim, Senior Software Engineer & Asrul Ash

Get the latest updates about our blog posts.

Subscribe so you don’t miss out!

Follow Lizard Global on Facebook, Instagram, and LinkedIn.


Software developers, programmers, and software engineers now depend on Git to log changes made to their projects in order to collaborate more effectively. One of Git's numerous built-in features, Git hooks is a wonderful method to use Git for information checking and task automation. Software developers will find it surprisingly simple to implement these powerful hooks on their own.


We'll go into further detail regarding Git hooks and their server-side and client-side applications in this article. Additionally, we will provide you with some instances of Git hooks you can use in your own projects. First though…


What are Git Hooks?

Scripts known as "git hooks" are executed automatically in a Git repository each time an event takes place. Programmers can tailor Git's internal behavior by automating some tasks at the program and deployment levels with Git hook scripts. This covers hook examples from any given Git repository, like these:


  • pre-commit.
  • prepare-commit-msg.
  • commit-msg.
  • post-commit.
  • post-checkout.
  • pre-rebase.

However, because GitHub needs you to be the repository owner or have admin access in order to create webhooks in that repository, and GitLab administrators configure server hooks on the file system of the GitLab server, these websites prohibit their use in the first place. Git hooks are therefore mostly utilized in local or private instances. On the other hand, pre-commit hook will be the main topic of this post.


What are the types of Git Hooks?

There are various git hooks, and each has a distinct function:


  • Client-Side Hooks (Local Hooks): pre-commit, commit-msg, prepare-commit-msg, and post-commit
  • Server-Side Hooks: pre-receive, update, and post-receive

While server-side hooks allow you to respond to different git push stages, client-side hooks (also known as local hooks) allow you to plug into the full commit life cycle. Furthermore, post-hooks are solely utilized for notifications, but pre-hooks allow you to change the action that is going to happen. Additionally, we'll learn some helpful methods for utilizing lower-level Git commands to obtain repository information and parse hook arguments.


But we'll be concentrating on Pre-Commit Git Hooks in this blog post. For what reason? Take a look below.


What is the use of Pre-Commit Git Hooks?

Developers can respond to various git events by using git hooks to listen for them. Before submitting changes to the remote repository, developers can use the pre-commit hook to double-check everything. It is used to check the upcoming commit snapshot, check that tests are running, check for mistakes, and look over any necessary code inspections.


Pre-commit hooks are designed to enhance commit quality. To do this, ensure that your commits adhere to certain formal constraints, such as following a specific code style (with the hook style files) that you use such as README.md or .


How to write a Pre-Commit Git Hook? (With examples)

Husky is a tool that makes it simple to manage Git hooks and execute the desired scripts at specific points. It functions by incorporating an item directly into the package.json file that sets up Husky to execute the designated scripts. Husky then takes care of controlling the stage in the Git lifecycle at which our scripts will execute. However, how will we build it?


In this article, we'll build a pre-commit hook to verify that the commit message contains the following prefixes:


  • feat
  • fix
  • BREAKING CHANGE(!)
  • test
  • style
  • refactor
  • Merge
  • hotfix

Be sure to install Husky in your Node.js application and initialize git in the project before configuring the hook. The following command can be used to install it if you haven't already:

npm install husky --save-dev

After installing the aforementioned package, add the following code to the package.json file's scripts section.

"prepare": "husky install"

Once the code mentioned above has been added to the package.json file, execute the following commands one at a time:


  • npm run prepare
  • npx husky add .husky/commit-msg
  • git add .husky/commit-msg

A file called commit-msg will be produced inside the .husky folder in the project root directory following the execution of the aforementioned scripts. Put the following code in place of the commit-msg file's contents.



Now a hook should run and check the format of the commit message each time you commit your modifications. For instance, if we do git commit -m "Something", the commit will fail and the console will display the message below.



And if we run git commit -m “fix(something)” then the changes should be committed without any error.


Need a hand?

With the use of hooks, programmers, engineers, and software developers can automate unique activities every time a particular event takes place in their Git repository. This article has discussed the various uses for local hooks, along with installation instructions and some examples of Git hooks.


We hope you've been able to make the most of Git by reading this article. However, if you have any queries or want to learn more about it, please contact our Software Developers for further information on Git and Git Hooks by clicking the button below.



Looking for Programmer Jobs in Malaysia and the Netherlands?


You are on our radar! Do you share our enthusiasm for technology and digital innovation, and do you have an eye for writing well-written software using the MERN tech stack? Our development team may be looking for someone like you! If you give us a call or get in touch with us on the button below, perhaps you’ll be part of the Lizard Family soon.


Frequently asked questions

English
English
Nederlands

01

What are Git Hooks?

Git hooks are shell programs that can be discovered in a Git repository's hidden.git/hooks directory. These scripts can assist Programmers in automating your development lifecycle by causing actions to be triggered in response to predetermined circumstances.

02

What are the types of Git Hooks?

There are various git hooks, and each has a distinct function:

  • Client-Side Hooks (Local Hooks): pre-commit, commit-msg, prepare-commit-msg, and post-commit
  • Server-Side Hooks: pre-receive, update, and post-receive

03

What is the use of Pre-Commit Git Hooks?

Some advantages of using Git hooks are maintaining a commit policy, preventing operations that don't adhere to established standards, and facilitating continuous integration workflows.

04

Why is the pre-commit hook a useful tool?

Enhancing commit quality is the aim of pre-commit hooks. This is accomplished by ensuring that your commits adhere to particular (formal) standards, such as following a specific coding style (using the hook style files) that you use such as README.md or other variants.

An image of markus at the blog page

Hey there, can I help you?

Did you like the blog above, but do you still have some questions about the subject or related topics? No issue! You can easily contact one of our Lizard specialists on these specific topics, and they gladly tell you more about it. This way, you’ll never leave with uncertainties.

MARKUS MONNIKENDAM

Global Commercial Director | markus@lizard.global | +60 18 35 65 702

Similar Articles