Git hooks are like handy helpers that let you run scripts (things like spell checks, linters, or automated tests) before or after important moments in your Git tasks, like when you're committing, merging, or pushing your code.
Git hooks often run simple tasks. This can be anything from ensuring that every developer follows the same naming rules for commits or branches. The awesome thing about Git hooks is that you can fine-tune everything in the scripts, resulting in a better experience for the developers.
Have you ever dreamed about consistency in your development team? Without sacrificing time and actually getting more sh*t done? Git hooks is the answer! 🔥
How To Get Started With Git Hooks?
Hooks are baked into Git, making it even easier for you to use them. If you go to your repository and clone it, then take a look inside the hidden
.git folder at the root of your project. In here you should be able to find a
Let's see what's inside the hooks folder by running a simple command like
ls -l. Here is the output:
christian@chs-laptop ~/R/s/.g/hooks (GIT_DIR!)> ll total 60K -rwxr-xr-x 1 christian domain users 478 okt 27 13:36 applypatch-msg.sample -rwxr-xr-x 1 christian domain users 896 okt 27 13:36 commit-msg.sample -rwxr-xr-x 1 christian domain users 4,6K okt 27 13:36 fsmonitor-watchman.sample -rwxr-xr-x 1 christian domain users 189 okt 27 13:36 post-update.sample -rwxr-xr-x 1 christian domain users 424 okt 27 13:36 pre-applypatch.sample -rwxr-xr-x 1 christian domain users 1,7K okt 27 13:36 pre-commit.sample -rwxr-xr-x 1 christian domain users 416 okt 27 13:36 pre-merge-commit.sample -rwxr-xr-x 1 christian domain users 1,5K okt 27 13:36 prepare-commit-msg.sample -rwxr-xr-x 1 christian domain users 1,4K okt 27 13:36 pre-push.sample -rwxr-xr-x 1 christian domain users 4,8K okt 27 13:36 pre-rebase.sample -rwxr-xr-x 1 christian domain users 544 okt 27 13:36 pre-receive.sample -rwxr-xr-x 1 christian domain users 2,8K okt 27 13:36 push-to-checkout.sample -rwxr-xr-x 1 christian domain users 3,6K okt 27 13:36 update.sample
Before all actions will run, the
pre- hooks are fired. Then you might think that the
post- hooks will be fired after, but they are only used for notifications. Below is an example, of what you could achieve:
The awesome thing about Git Hooks is that they are not checked in your source control and you can have as many custom stuff going on as you would like. By default, all hooks will be copied from the
templates/hooks, folder located at your global Git installation.
If you take a closer look at the scripts in the example above, you might notice that there is a
.sample extension on each of them. This makes them disabled by default.
Different types of Git Hooks
As I mentioned before, there are different kinds of Git Hooks you can make use of. We both have client- and server-side Git Hooks and some of the most useful are listed below.
The most useful (in my opinion) client-side git hooks are:
The most used and useful git hooks I see used on servers are the following. Please note that these git hooks are related to different stages in the push process of a new commit. The naming makes it quite obvious where these hooks are fired in the process.
If you would like to read the in-depth documentation for git hooks you can take a closer look at the official documentation available at the link below.
I always make my git hooks in bash, but actually, you can make them in almost any kind of language. Python would probably be my second choice as it is a very good scripting language.
Creating a Git Hook from Scratch
If you would like to create a new Git Hook from scratch, it's fairly easy! The only thing you have to remember is that the filename must match of of the hooks we saw earlier. When you are done, just make it executable using a command like the one below:
chmod +x prepare-commit-msg
If you would like to see an example of how you could implement a Git Hook, here is an example for you, where I have created a