Git Hooks: Using Git to Take Action
- Matt Zaske
- March 02, 2026
- 3 minutes
I've recently spent a fair bit of time in Git and GitHub cleaning up and migrating repositories as part of a work-related project. During this cleanup, I discovered the first "test repository" I used while learning about and working through some details related to my five-year-old post about Git-ting the hang of hooks.Why I'd used my work account for that is unclear, but I decided this was as good as any time to clean it up and turn it into an example repo for folks to try out (and move it to my personal GitHub account in the process).
What Are Git Hooks?
At their core, Git Hooks are script blocks you can use to Do Things when triggered by one of several client-side or server-side actions. The documentation covers all of those individual triggers, but I think of them as a form of CI/CD action where a more traditional CI/CD practice doesn't quite fit. Or other weird situations (like what I wrote about in the old post).
So I Made An Example Repo
The example repo is pretty straightforward and can be used to directly and immediately illustrate how Git Hooks can be used. The README has some of the story and explains details about how things are configured. I created the repo with three branches:
main(the default);dev; andprod.
Hooks are then installed and automatically triggered when either switching to (post-checkout) or pulling to (post-merge) the dev and prod branches only. Being on main has no effect due to how I have the hook scripts set up. Switching between branches copies the two files in question into their "production" (included) location for either environment. The final part of the README outlines the steps to run this on your own:
- Clone this repo; by default you'll be on the `main` branch.
- Copy the
post-checkoutandpost-mergefiles from thez_deployscripts/folder into the.git/hooksfolder. git checkout dev(or otherwise change to thedevbranch) and notice two files appear in theincludes/folder:app_conf.phpheader.php
These files will be identical to their.devcounterparts!
git checkout prod(or otherwise change to theprodbranch) and notice the contents ofapp_conf.phpandheader.phpwill now be identical to their.prdcounterparts!- Switch between branches to your heart's content and watch the magic. The ignored files will change according to the current branch. Note how when you switch to the
mainbranch the files do not change since themainbranch does not match one of the hook patterns!
A Niche Use Case, But Useful Information
I've mentioned Git Hooks to folks in my MMS sessions, but also in the context of "they are cool, but I would not use one as a first attempt at a solution." They also don't scale well since hooks have to be manually installed for each clone (assuming client-side hooks), so using Hooks within a team setting could become a pain point during setup.
They can be the right tool for the job, though, and honestly in the very small number of places I've used Hooks they have been rock solid. Good luck!






