Git Tutorial

Git Hooks Crash Course

Author: Emad Zaamout

Monday, April 19, 2021 (MDT)

Table of Contents

  1. Introduction to Git Hooks
  2. What is git hooks?
  3. Git client-side hooks
  4. Visual Code - Show hidden folders
  5. Git Remote Repository – Add Remote Origin
  6. git pre-commit hook
  7. git commit-msg hook
  8. git pre-push hook
  9. git-hooks-final-notes

Introduction to Git Hooks

Welcome back

Are you using Git hooks? Git hooks can reduce 99% of all your pipelines failures (if not all) by running the same tasks (I.e., unit tests, lint scripts, etc. ...) locally, on the developer machine before code is committed or pushed.

Since pipelines consume allot of resources and time. There is nothing worse than running a pipeline only to fail on a lint script or unit test. By running your checks first on the developer machine before code is pushed to your repository, you can reduce all those errors.

In this tutorial, we are going to cover Git Hooks and why you should be using them.

If you haven’t, don’t forget to like and subscribe to stay up to date with our latest training videos.

What is Git Hooks?

Git hooks are a built-in feature in git.

Git Hooks are scripts that run before or after certain events are triggered. If the git hooks scripts fail, then it prevents the action from happening.

For example, you can prevent any code from being pushed if it fails running lint scripts or your automated tests.

There is 2 groups for Git Hooks, server-side hooks and client-side hooks.

  1. Server-side hooks: are triggered before or after certain events or actions happen your Remote Git Repository.

  2. Client Side: Triggered on your local Git Repository

For example, you have a local git repository, and you have a remote git repository. Now let’s stay you have some files that you want to commit and push to your remote repository. Here’s what happens.

On your local repository, before your code is committed, a pre-commit hook runs. If it fails, you can’t commit code. You can use pre-commit events to check for code standards by running lint.

If your pre-commit hook passes, then your code is committed to your local branch.

After your code is committed, a post-commit hook runs. You can use this hook to run your unit tests or other task that you want before code is pushed to your remote repository.

Now once your code is pushed on your remote repository, you have a pre-receive hook that runs before your repository is updated. If you pre-receive hook fails, then the code push is rejected.

Finally, once all that is done, you have a post-receive hook that runs. You can use this hook to send notifications or anything else after code is pushed and received. You can also do things like run deployment scripts.

This is just an example, there are allot of other hooks that we will briefly cover. You don’t need to memorize all this. They are already stored inside your .git/hooks folder.

Git client-side hooks

Client-side hooks run locally so if you have git installed then your all set. Otherwise, you will want to install Git. If you don’t know how to install Git, then I recommend you watch our Git Crash Course tutorial video.

When you initialize a new repository by running git init, git populates the hooks directory with sample scripts.

Note, since we never store .git folder inside our remote repository, any change or update you make inside this folder, is ignored by your gitignore file.

So, all the changes you make here, will not be stored in your remote repository.

If you want to create client-side hooks and have them attached to your project, then you will need to create a script that adds your hooks inside your .git/hooks folder.

If you have an install or build script for your project, then you attach that script there. Otherwise, you can add it to your composer file and run it every time composer install runs.

Now, let’s look on what’s inside the .git/hooks folder. Notice Here we some predefined hooks for us. They are all sample scripts for each action.

To activate these hooks, you will need to remove the .sample from the name.

Visual Code - Show hidden folders

Let’s go ahead and open our project directory inside our code editor. I am using Visual Code. if your using Visual Code, then you most likely won’t see your .git folder. That’s because by default this folder is excluded.

Let’s temporarily remove the exclude rule . To do that, you want to open your settings (Preference > Settings), then search for “files.exclude”.

You should be able to see that .git folder is excluded. Once you remove this pattern you should be able to see it .git folder on your explorer tree.

Git Remote Repository – Add Remote Origin

It does not really matter which git remote you use, for this tutorial I am using Bitbucket. Before we start adding hooks lets go ahead and create a remote repository so we can test our hooks.

To add a remote origin use: git remote add origin remote_url

git pre-commit hook

The pre commit hook runs before every commit. To create a pre-commit hook, were going to rename the file inside ./.git/hooks/pre-commit.sample to pre-commit

Now modify that file and remove everything. Add echo “PRE COMMIT HOOK” to display a message. Now lets go ahead and commit a new change. To skip the pre commit check, you run the same commit command but with --no-verify flag: git commit -m “test message” --no-verify

git commit-msg hook

commit-msg is invoked by git commit. This relates to the commit message. You can use this hook to check if the commit message meets your standard. For this example, lets write a quick rule to prevent any message from being empty.

git pre-push hook

The pre push commit runs right before our code is pushed to our remote repository. You can use this hook to run your unit tests and any other task.

git-hooks-final-notes

I hope you guys get the point. It’s redundant to go through each single one. These are all the other hooks that you could use. They are all listed here. If you guys have any questions, feel free to write a comment. If you guys found this tutorial useful, then I would appreciate if you hit the like button. Also, don’t forget to subscribe to our channel to stay up to date with our latest training videos.

Other Posts

Windows WSL 2 Docker Tutorial Course Image

Tuesday, August 22 2023

Deploy any Dockerized application using AWS Course

Author: Emad Zaamout
Amazon Elastic Container Registry (AWS ECR)

Tuesday, June 27 2023

Amazon Elastic Container Registry (AWS ECR)

Author: Emad Zaamout
Custom Docker Images Course

Tuesday, June 27 2023

Custom Docker Images

Author: Emad Zaamout
Laravel Makefiles Course Image

Sunday, Oct 24, 2022

Laravel Makefiles

Author: Emad Zaamout
Windows WSL 2 Docker Tutorial Course Image

Sunday, Oct 24, 2022

Laravel Docker Course

Author: Emad Zaamout
Windows WSL 2 Docker Tutorial Course Image

Sunday, Oct 24, 2022

Laravel 9 Complete Course | Blog Implementation

Author: Emad Zaamout
Windows WSL 2 Docker Tutorial Course Image

Sunday, Oct 24, 2022

Windows WSL 2 Docker Tutorial

Author: Emad Zaamout
GIT Crash Course using Bitbucket By Emad Zaamout

Saturday May 1, 2021

Laravel Websockets Example Chat Application

Author: Emad Zaamout
GIT Crash Course using Bitbucket By Emad Zaamout

Saturday May 1, 2021

Laravel API Course | MVCS Repository Pattern

Author: Emad Zaamout
GIT Crash Course using Bitbucket By Emad Zaamout

Saturday October 24, 2021

Git Tutorial - Git Crash Course using BitBucket

Author: Emad Zaamout
What is AWS Elastic Load Balancer By Emad Zaamout

Monday October 18, 2021

AWS Elastic Load Balancing

Author: Emad Zaamout
DMARC SPF DKIM Course By Emad Zaamout

Saturday October 16, 2021

Email DNS Master Course - SPF + DKIM + DMARC

Author: Emad Zaamout
Email SPF Record Tutorial – Sender Policy Framework (SPF) | Prevent Email Spoofing | DNS Course By Emad Zaamout

Saturday October 16, 2021

Email SPF Record Tutorial – Sender Policy Framework (SPF) | Prevent Email Spoofing | DNS Course

Author: Emad Zaamout
DMARC Tutorial - How to set up DNS DMARC record | Protect Your Doman By Emad Zaamout

Saturday October 16, 2021

DMARC Tutorial - How to set up DNS DMARC record | Protect Your Doman

Author: Emad Zaamout
Git Hooks Crash Course

Sunday, September, 2021 (MDT)

Git Hooks Crash Course

Author: Emad Zaamout
Laravel CI\CD using AWS RDS EC2 S3 CodeDeploy BitBucket By Emad Zaamout

Friday, September 17, 2021 (MDT)

Laravel DevOps Tutorial - Laravel Deployment Automation CI\CD using AWS RDS EC2 S3 CodeDeploy BitBucket

Author: Emad Zaamout
Deploy any Laravel app in AWS (Amazon Web Services) By Emad Zaamout

Monday, April 19, 2021 (MDT)

Deploy any Laravel App in AWS (Amazon Web Services)

Author: Emad Zaamout
Fisher Yates Shuffle Algorithm Implementation? By Emad Zaamout

Saturday, September 26, 2020 (MDT)

Find out the secrets, tips and tricks to ranking number 1 on Google.

Author: Emad Zaamout
Fisher Yates Shuffle Algorithm Implementation? By Emad Zaamout

Saturday, September 26, 2020 (MDT)

Fisher - Yates Shuffle Algorithm Implementation

Author: Emad Zaamout
What Is an Ecommerce Website & How to Get Started (2020 guide)? By Emad Zaamout

Saturday, September 26, 2020 (MDT)

What Is an Ecommerce Website & How to Get Started (2020 guide)?

Author: Emad Zaamout
5 Reasons Why You Need A Website Calgary Website Design Company AHT Cloud

Thursday, May 7, 2020

5 Reasons Why You Need A Website

Author: Emad Zaamout
Whats Involved in Creating a Unique Custom Website? By Emad Zaamout

Thursday, May 7, 2020

Whats Involved in Creating a Unique Custom Website?

Author: Emad Zaamout
SEO Checklist By Emad Zaamout

Thursday, May 7, 2020

SEO CHECKLIST

Author: Emad Zaamout

GET YOUR FREE ESTIMATE

CONTACT US TODAY FOR YOUR FREE CONSULTATION!


Contact us today to discuss your goals and we will create a simple roadmap to get you there. We look forward to speaking with you!

Main Office

Phone:   1 587-834-6567
Email:   support@ahtcloud.com
32 Westwinds Crescent NE #130
Calgary, AB T3J 5L3, CA

Products

TMS
Cloud Based Transportation Management System


Hours Of Operation

Monday 8:00 am - 5:00 pm
Tuesday 8:00 am - 5:00 pm
Wednesday 8:00 am - 5:00 pm
Thursday 8:00 am - 5:00 pm
Friday 8:00 am - 5:00 pm
Saturday Closed
Sunday Closed