DEV Community

eranelbaz
eranelbaz

Posted on • Edited on

Git Hooks 101

In this tutorial I won't focus on what git hooks are, I'll show u simple example on how you can use them

What git hooks are?

From git documentation:

Hooks are programs you can place in a hooks directory to trigger actions at 
certain points in git’s execution. Hooks that don’t have the executable bit set 
are ignored.

and in other words:

  • hook is program that run when event occurs
  • there is a directory with all the hooks

All the event are listed here

In this tutorial I want to focus on Pre-Commit event

Sample Hooks

In our .git/hooks folder we can find all the default hook samples:

$ ls .git/hooks
applypatch-msg.sample      post-update.sample     pre-push.sample     prepare-commit-msg.sample
commit-msg.sample          pre-applypatch.sample  pre-rebase.sample   update.sample
fsmonitor-watchman.sample  pre-commit.sample      pre-receive.sample

Our Hook

In this 101 tutorial I'll show you how to create simple git hook that prevent git commit command to master branch

Lets now create new file pre-commit without any file extension

$ touch .git/hooks/pre-commit

Now because git runs under linux (for windows using MinGW) the first line needs to be Shebang

For Linux:

#!/bin/bash

For Windows it should be you git bash location, For example:

#!C:/Program\ Files/Git/usr/bin/sh.exe

next we need to execute git branch command in order to print our branch, but we want to know if we are currently on master by using grep:

git branch | grep "* master"

Now we warp with if with message:

if git branch | grep "* master" > /dev/null 2>&1
then
    cat <<\EOF
# Message
EOF
fi

in order to disable the commit we need to provide exit code which is not 0:

exit 1

And our hook for pre-commit looks like that:

if git branch | grep "* master" > /dev/null 2>&1
then
    cat <<\EOF
Commits are not allowed on branch Master!
EOF
    exit 1
fi

lets try:

$ git commit -m "Commit message"
Commits are not allowed on branch Master!

Make it as Default

What is the problem with most people? is that we are lazy, we want to do something only once

and we have this option using git template directory that you can find more about it here

what that this tempalte direcotry does is when you are using git init command it will copy all the files under this directory whose name do not start with a dot

now lets define our template dir:

$ export GIT_TEMPLATE_DIR=PATH
$ cd PATH
$ mkdir hooks

and under PATH\hooks create your hooks and enjoy :)

Like this post?
Support me via Patreon
Subscribe to my YouTube Channel

Top comments (2)

Collapse
 
greatbahram profile image
Bahram Aghaei

Well done!

I hope, in the future, you cover how to share hooks with other contributers, I mean tools like pre-commit or the others.

Collapse
 
eranelbaz profile image
eranelbaz

There are multiple ways, if you are using Javascript you can use Husky,
else we are using .githook and redirect git config core.hooksPath to this folder,
but it needs to be done for each member on his local repository, still couldn't find a way to make it automatically