![]() |
Understanding Git Hooks and How to Use Them in Your Workflow |
Git hooks are like those little automation elves that quietly work behind the scenes, making your life easier. Ever wanted to stop a commit because you forgot to format your code? Or maybe you want to send a Slack notification whenever someone pushes to the repo. That's where Git hooks come in.
What the Heck Are Git Hooks?
Alright, so Git hooks are basically scripts that run automatically when certain Git events happen. Things like committing, pushing, merging—those moments where you wish there was a way to enforce rules or automate repetitive tasks.
These hooks live inside your .git
directory, specifically in .git/hooks
. Out of the box, Git already includes some example hooks. But they just sit there, disabled, waiting for you to bring them to life. You can customize them to do whatever you need.
Types of Git Hooks
You’ve got two categories of hooks: client-side and server-side. One happens on your machine. The other runs on the remote repo.
Client-Side Hooks
These run before or after operations like committing, merging, or checking out branches. Some of the common ones:
- Pre-commit → Runs before you even type your commit message. Perfect for things like linting or checking for debug logs.
- Commit-msg → Fires right after you enter your commit message. You can use it to enforce commit message standards.
- Post-commit → Runs after the commit is done. Handy for notifications or logging.
- Pre-push → Kicks in before you push to a remote. Can prevent bad code from making it to production.
Server-Side Hooks
These live on the remote repository. They're good for stuff like enforcing policies.
- Pre-receive → Checks the incoming push before it’s accepted.
- Update → Happens per branch. Can reject updates to certain branches.
- Post-receive → Happens after the push lands. Good for sending notifications.
How to Use Git Hooks in Your Workflow
The fun part! Let’s say you wanna prevent committing code with console.log()
in it. You’d use a pre-commit hook.
Setting Up a Pre-Commit Hook
1. Navigate to your Git hooks directory:
cd .git/hooks
2. Create a new file called pre-commit
:
touch pre-commit
3. Open it and add this script:
#!/bin/sh
if grep -r --exclude-dir=.git "console.log" .
then
echo "🚨 You left a console.log() in your code! Commit rejected."
exit 1
fi
4. Make it executable:
chmod +x pre-commit
Now, every time you try to commit, Git will scan your files. If it finds console.log()
, it blocks the commit and throws a warning.
Another Cool Use Case – Enforcing Commit Message Format
Your team probably has some kind of commit message standard. Maybe you prefix commits with feat:
, fix:
, or chore:
. A commit-msg hook can enforce that.
Here’s how:
1. Create a file named commit-msg
inside .git/hooks
2. Add this script:
#!/bin/sh
COMMIT_MSG_FILE=$1
COMMIT_MSG=$(cat $COMMIT_MSG_FILE)
if ! echo "$COMMIT_MSG" | grep -E "^(feat|fix|chore): " > /dev/null
then
echo "🚨 Commit message must start with feat:, fix:, or chore:."
exit 1
fi
3. Save it and make it executable with chmod +x commit-msg
Now, if someone tries to commit without the proper prefix, Git smacks them with a warning and rejects the commit.
Going Beyond – Supercharging Your Workflow
Git hooks aren’t just for blocking bad commits. You can:
- Automatically run tests before a push to prevent broken code from hitting the repo.
- Send a Slack notification when a push happens.
- Format your code before every commit, so you never have to worry about styling again.
- Keep secrets out of the repo by checking for
.env
files before committing.
Example: Running tests before a push? Just create a pre-push hook:
#!/bin/sh
npm test
if [ $? -ne 0 ]
then
echo "🚨 Tests failed. Push rejected."
exit 1
fi
Now, every time you push, Git runs your tests. If they fail, the push gets denied.
Final Thoughts
Git hooks are like magic automation spells for your repo. They help enforce rules, keep things clean, and make sure no one (including you) forgets important steps. If you're not using them yet, you’re missing out. Start small. Add a pre-commit hook. Then explore more advanced automation. Soon enough, you’ll wonder how you ever lived without them.
So go ahead. Hack your Git workflow. Make it work for you.