Git Hooks — The Missing Tutorial

Automate stuff? That’s what programming should be about

Using Git Hooks is just like having automated robots who do whatever you want, when a command is run in Git.

Now, since you should be running Git anyway (unless you are running some other version control software) so it is like you’ve got slaves working for you for free! How great is that!

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • The tutorial is designed for the Mac, and refers to commands using Mac OS
  • Be able to use VIM to make changes to a file (Guide HERE)
  • Have some familiarity with Git (Common commands are HERE)
  • Have a reason to automate something along with your Git commands (although some suggestions are listed below)

Terminology

Git: Git is a distributed version-control system for tracking changes in source code during software development

Git Hooks: Programs you can use to tigger actions at points in Git’s execution

Script: A program which is carried out by another program

Vim: This is a contraction of Vi and IMproved. That is, it is a clone (with additions) of the vi text editor for Unix. In other words, it is a command-line interface for creating and manipulating files

The uses of Git Hooks

Git Hooks allow you to run scripts as commands are run using Git from the command line.

There are loads of opportunities to use this, and get automation working for you.

Here are some ideas:

Enforce preconditions for merging

Make all programmers working on a repository complete their TODOs before committing. No putting off work until tomorrow!

Enforce the format of commit messages

Solving a JIRA ticket (or other issue tracking system)? Make sure that all commit messages reference the ticket. Great.

Email confirm your push

Get a confirmation for your push. So you know that it has happened.

Run tests when you commit

Run some tests on your code every-time you commit? That can happen. Yes it can!

Getting up and running

Using Git Hooks isn’t all that difficult, especially since some are provided when you set up a repository. Really!

Open Terminal

We can open terminal using Spotlight (pressing Command-Space) and type Terminal into the space.

Create a file to commit later

We are going to create a readme.MD file. It doesn’t matter if it contains anything (so I’m not going to bother putting anything into it

touch readme.MD

will do it!

Setting up a Git Repo

If you haven’t already, you can set up a Git repository.

So traverse to a good place on your drive (documents would be a good spot):

cd ~/Documents

create a new directory

mkdir hooks

move into the new folder

cd hooks

then setup a new git repo

git init

This creates a hidden .git folder in your hooks folder. Don’t worry, using Terminal we can see it.

Usually we would use ls to see files, here we can use the -f argument or even better -la to get a more detailed view

ls -la

you can now see there is a .git folder — and it is this we can traverse to

cd .git

we can see the folders within the .git folder, and we want to get into the hooks folder. So we first confirm the folders

ls -la

so we move to the hooks folder

cd hooks

we can then look at all of the files in the folder

Image for post
Image for post

Enabling any of the examples

You can look through the examples and see what they do. A rename and use of Vim can get us through this!

We can first rename

cp pre-commit.sample pre-commit

Now we can make the file executable

chmod +x pre-commit

And then we can open the file in Vim

Vim pre-commit

You can see that the script will parse the git commit message. However, we are going to write our only slightly simpler message in the next section.

Create our own statement

If you still have VIM open you can delete the existing text.

We’re just going to have a play around! Let us try to add the following

#!/bin/sh
echo "pre-commit"
exit 0

Now exit 0 makes the commit and exit 1 does not make the commit (guess what we’ll change later).

In any case save the file by pressing escape and then

:wq

which saves the file.

We can then traverse back up two folders through the following

cd ..
cd ..

now we are going to add our files before the commit!

git add .

and finally commit with a first message

git commit -am "first message"

which lets us see our wonderful “pre-commit” message, and the commit has happened!

So what happens if we exit 1?

We’ll need to make a change to the readme

vim readme.MD

and write changed or similar.

escape and

:wq

to save.

We can now Vim straight to the pre-commit file

vim .git/hooks/pre-commit

and change the file to

#!/bin/sh
echo "pre-commit"
exit 1

Now once again we will

git add .

and write a commit message

git commit -am "first message"

this time we are met with just the message pre-commit but the file is not commited. This all seems simple, right?

Where do we go from here?

Loads of places. You’ll want to use if statements.

These look something like:

if [-d $variable == "string" ]; then
echo "something"
else
echo "something else"
fi

there is also a fun printf command that gives more control than echo

printf

and we can even read files

FILE=filename
variable=$(grep -i "var" -w $FILE | cut -d -f2)

That’s enough to give you some power over what can happen. But remember pre-commit is not the only option we have for the Git Hooks and we can even look at the examples that we have been given in the Git folder.

Wonderful

Conclusion:

Git Hooks allow you to run scripts before commits, push and other commands to make testing easier or making your coding more disciplined.

You do have to write some scripts, but in the end the effort is worth it to make your coding experience better.

It just takes some work first, but when you’ve done it you’ll certainly be grateful!

Extend your knowledge

  • There is a rather in-depth tutorial for Git Hooks HERE

The Twitter contact:

Any questions? You can get in touch with me here

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store