
What Is the Purpose of Git Hooks? A Beginner-Friendly Guide
Scott Keck-Warren • May 16, 2025
Git is one of those tools that seems simple on the surface but then becomes more and more powerful the more you work with it and one of its lesser-known features is Git Hooks. Git Hooks are custom scripts that allow developers to automate tasks and enforce standards at different points in the Git workflow. In this post, we'll explore what Git hooks are, why they matter, and how they can improve your development process.
What are Git Hooks?
Git Hooks are customizable scripts that Git will execute at specific points in its lifecycle. This allows you to automate crucial tasks and enforce rules before specific actions occur. These hooks can be used to streamline your workflow, ensure your code has the high quality you expect, and facilitate CI/CD.
What Is the Purpose of Git Hooks?
The main purpose of Git hooks is to automate tasks and enforce coding standards during Git operations like committing, pushing, or merging. They help:
- Ensure code quality before it’s committed
- Run tests or linters automatically
- Block bad commit messages
- Streamline team workflows
- Support CI/CD pipelines by triggering actions at key moments
By integrating Git hooks into your workflow, you reduce human error and create a more consistent codebase.
Where Do Git Hooks Live?
Inside our repository hidden from us by default is the ".git" directory. The ".git" directory is where Git stores all of the files that it needs in order to work.
ls -l .git/
total 848
-rw-r--r-- 1 scottkeck-warren staff 388 May 14 20:53 COMMIT_EDITMSG
-rw-r--r-- 1 scottkeck-warren staff 3778 May 12 20:51 FETCH_HEAD
-rw-r--r-- 1 scottkeck-warren staff 31 May 14 20:53 HEAD
-rw-r--r-- 1 scottkeck-warren staff 41 May 12 20:51 ORIG_HEAD
-rw-r--r-- 1 scottkeck-warren staff 2320 May 14 20:53 config
-rw-r--r-- 1 scottkeck-warren staff 73 Oct 17 2024 description
drwxr-xr-x 16 scottkeck-warren staff 512 Feb 17 10:25 hooks
-rw-r--r-- 1 scottkeck-warren staff 405221 May 14 20:53 index
drwxr-xr-x 3 scottkeck-warren staff 96 Oct 17 2024 info
drwxr-xr-x 4 scottkeck-warren staff 128 Oct 17 2024 logs
drwxr-xr-x 260 scottkeck-warren staff 8320 May 14 20:53 objects
-rw-r--r-- 1 scottkeck-warren staff 1720 Oct 17 2024 packed-refs
drwxr-xr-x 5 scottkeck-warren staff 160 May 12 20:51 refs
In general, you don't want to mess with these files directly because it can (and most likely will) destroy your repository.
The directory we're interested in is the "hooks" directory:
% ls -l .git/hooks
total 128
-rwxr-xr-x 1 scottkeck-warren staff 478 Oct 17 2024 applypatch-msg.sample
-rwxr-xr-x 1 scottkeck-warren staff 896 Oct 17 2024 commit-msg.sample
-rwxr-xr-x 1 scottkeck-warren staff 4726 Oct 17 2024 fsmonitor-watchman.sample
-rwxr-xr-x 1 scottkeck-warren staff 189 Oct 17 2024 post-update.sample
-rwxr-xr-x 1 scottkeck-warren staff 424 Oct 17 2024 pre-applypatch.sample
-rwxr-xr-x 1 scottkeck-warren staff 1643 Oct 17 2024 pre-commit.sample
-rwxr-xr-x 1 scottkeck-warren staff 416 Oct 17 2024 pre-merge-commit.sample
-rwxr-xr-x 1 scottkeck-warren staff 1374 Oct 17 2024 pre-push.sample
-rwxr-xr-x 1 scottkeck-warren staff 4898 Oct 17 2024 pre-rebase.sample
-rwxr-xr-x 1 scottkeck-warren staff 544 Oct 17 2024 pre-receive.sample
-rwxr-xr-x 1 scottkeck-warren staff 1492 Oct 17 2024 prepare-commit-msg.sample
-rwxr-xr-x 1 scottkeck-warren staff 2783 Oct 17 2024 push-to-checkout.sample
-rwxr-xr-x 1 scottkeck-warren staff 3650 Oct 17 2024 update.sample
Each of these files contains a sample of what we can do with each of the hooks. Some of my favorites are:
commit-msg
- which allows you to validate your commit message matches a standardpost-checkout
- which allows you to perform postgit checkout
andgit switch
actionspre-commit
- which allows you to perform validations before Git creates a commit
Now one of the downsides to the "hooks" directory is that it's not part of the repository so it's not 100% enforced and it's not automatically updated when we do pulls and switching of branches. To get around this I generally store the scripts so they can be dependent on the current branch and then just copy them into the correct location (this could be done with a post-checkout
hook but I'll show you how I prefer to do it in a future post).
Frequently Asked Questions
Are Git Hooks Shared Across Teams?
No. By default, Git hooks are stored locally in the .git/hooks
directory and aren't shared unless explicitly copied into the repo.
Can Git Hooks Be Version Controlled?
Yes! Many teams store hooks in a versioned folder like scripts/hooks/
and copy them into .git/hooks
using a setup script or a tool like Husky
.
Do Git Hooks Run on GitHub?
Git hooks only run locally. If you want similar automation on GitHub, you’ll need to use GitHub Actions or other CI tools.
This was originally published at https://phpdeveloperstv.substack.com/p/what-is-the-purpose-of-git-hooks