Open In App

How to Install GIT Hooks?

Last Updated : 08 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Git hooks enable us to execute our custom scripts in response to specific git lifecycle events, like merging, committing, pushing, etc. This means that with the help of the scripts we can fire off certain events every time a git event occurs. For example, if you want to make a commit that follows specific criteria or anything special you want when that commitment is made, you can do that with those custom scripts. Hooks can be present on the local repos or server-side repos.

Exploring Git Hooks

I will be using git bash to interact with git on my system, make sure you have basic knowledge of Linux commands such as ls, PWD, etc. Let’s create a sample directory and initialize a git repository there.

mkdir mydir

cd mydir

git init 

The above commands create an empty directory named mydir and a git repo is initialized there.

Creating-empty-directory-named-mydir

 

When you type the “ls -a” you will see the hidden files as well as the .git folder. Git hooks are present in the .git folder.

ls -a

cd  .git/

Typing-is-a

 

.git-hooks

 

Now let’s go into the hooks directory and look at the contents.

hooks-directory

 

Inside the hooks directory, you can see that the files have an extension of “sample” which means that they are not yet usable. The pre and post-before file names suggest the timing when the scripts will be executed. For example, a pre-commit hook will work before we try to commit something and a post-commit hook will work after the work has been committed. To install a hook we just have to get rid of the .sample extension and give it permission to be executable.

The top 5 most popular hooks are:

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

Let’s try to install a hook and get practical experience. We will be installing a hook named pre-commit hook. First, we have to remove the .sample extension to make it usable and then make it executable.

mv pre-commit.sample pre-commit (removes the .sample extension)

chmod +x pre-commit (makes it executable)

.sample-extension-removed

 

Now that we have enabled this hook, we will use it to check whether the user email that we are using for committing is valid or not. If they are not correct then this hook will throw an error message. Open the pre-commit file with vi-editor and write this piece of code,

# Check if the email is set properly or not
useremail=$(git config user.email)
if [ “$useremail” != “mukulsharma3779@gmail.com” ]
then
      cat <<\EOF
Error: The user email you entered should be “mukulsharma3779@gmail.com”

EOF
         exit 1
fi

Writing-code-in-pre-commit-file

 

This piece of code ensures that the email address that you type must be correct, exit 1 at the end of the code means that we exited with some error. Now create a sample file GFG.txt.

Creating-file-sample

 

Now let’s try to commit this file, for committing I am using a sample email “audreyblake314@gmail.com”. To unset your email use the command below and log in again with another command.

git config –global –unset user.email <your_present_email> (mukulsharma3779@gmail.com)

git config –global user.email <dummy_email> (audreyblake314@gmail.com)

unsetting-email

 

Since the email is a different one, we are getting an error that we wrote in the pre-commit file. So this is how we learned to install a hook and also learned to practically use it. Similarly, you can use the other hooks by removing the .sample extension and writing your custom scripts.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads