Git Hooks Implementation in Laravel: Unit Testing & Prettier Automation

Cerwyn Cahyono
3 min readJun 22, 2021

As a web developer, we’ll be always working with the team. We want to make sure that our codes have the prettier standard and minimalizing bugs pushed into our git repository. As we already know, we can do those implementations using CI/CD to test our app before the deployment happens. But the question is, can we make that automation run locally whenever developers pushed the codes into the git? So we’re sure that every code that is pushed by developers is bug-free and also has prettier standardization.

We can create & implement these git hooks using the Husky library in our projects, where we set up some things to do before (or after) we commit/push our codes into the version control. We can run any commands that are needed before developers commit/push their codes. So in this article, I’ll show you how to create git hooks before we commit/push codes into the git where we’ll reformat our codes using lint-staged and run the tests inside the Laravel project.

Photo by Marvin Meyer on Unsplash


At the end of this article, we’ll make 2 hooks: pre-commit and pre-push hooks to the version control.


It’s a super useful feature so we can make sure that all the codes that are pushed into the version control are passed the tests. Of course, assuming that developers won’t skip this automation.


Let’s get started. First thing first, let’s install the Husky and lint-staged packages.

npm install husky lint-staged --save-dev
npx husky install

Then, for the PHP prettier, we need another composer package.

composer require friendsofphp/php-cs-fixer

Pre-Commit: PHP Prettier Hook

Let’s start with configuring the prettier. First, we need to make a git hook with Huksy when developers do the commit.

npx husky add .husky/pre-commit “npx lint-staged

A new file will appear in the root directory named .husky/pre_commit




Cerwyn Cahyono

PHP/Backend Engineer at Undercurrent Capital Pte Ltd — Data Science Enthusiast