🪝Git Hooks Made Simple
Automate Your Workflow with Pre-Commit and Post-Push Hooks
Welcome, friends! 👋
Today, we’ll learn about Git Hooks — what they are, how they work, and how to create your own.
Git Hooks let your repository take action automatically — from running tests to formatting code before a commit.
In this guide, we’ll simplify hooks so you can boost productivity with minimal setup.
🪝What Are Git Hooks?
Git Hooks are scripts that run automatically when certain Git events happen — like committing, pushing, or merging code.
They can:
- Run checks before committing code (
pre-commit) - Stop bad commits or pushes (
pre-push) - Trigger actions on the server (
pre-receive)
Local vs Global Hooks
- Local hooks: Only affect one repository.
- Global hooks: Apply to all repositories on your machine.
🛠 Creating Your First Git Hook
- Open your project folder in VS Code or any IDE.
- Initialize Git:
git init- Open the hidden
.git/hooksfolder. - Rename
pre-commit.sampletopre-commit.
Make it executable:
chmod u+x .git/hooks/pre-commitEdit it with a simple script:
#!/bin/bash
echo "Hello World! My first Git Hook 🎉"Now, try committing something:
git add .
git commit -m "test"You’ll see your hook message appear!
⚠ Blocking Bad Commits
You can make hooks that stop bad code — for example, blocking commits with trailing whitespace or hardcoded secrets.
Get J3’s stories in your inbox
Join Medium for free to get updates from this writer.
Here’s an example that stops AWS keys from being committed:
#!/bin/bash
if git grep -q "AWS_ACCESS_KEY"; then
echo "🚫 Secret detected! Commit blocked."
exit 1
fiツ Add Fun to Your Workflow
You can even reward yourself with a dad joke every time you commit:
# 😂 Add Fun to Your Workflow
curl https://icanhazdadjoke.comꗃ Advanced Example: Secret Detection Tool
Use GGShield to automatically detect secrets:
ggshield secret scan pre-commitWe’ll dive into this in the next post. Stay tuned!
🗺 Global Git Hooks
To make a hook apply to all repos:
- Create a folder:
mkdir -p ~/.git/hooksSet it as the global hook path:
git config --global core.hooksPath ~/.git/hooksAdd your pre-commit file there (just like before).
Now every repository you work on will run your hook automatically.
⤴︎ Level Up
You now know how to:
✅ Create Git hooks
✅ Run scripts automatically
✅ Prevent bad commits
✅ Apply hooks globally
To go further, check out the pre-commit framework — a popular way to manage and share hooks easily.
🎖 Pro tip:
Every time you commit, you’re not just saving code — you’re becoming an even better programmer! 💪
®️ Credits & References
Complete guide to GitHooks — Creating your own pre-commit hooks by GitGuardian
The secret to stop hardcoded secrets by gitguardian.com
8.3 Customizing Git — Git Hooks by git-scm.com
pre-commit — A framework for managing and maintaining multi-language pre-commit hooks by pre-commit.com










