Sitemap
Jungletronics

Education & STEAM Learning by building. Practical explorations in AI, programming, electronics, robotics, IoT, and other STEAM topics — from ideas and experiments to working products. Follow and build with us.

🪝Git Hooks Made Simple

Automate Your Workflow with Pre-Commit and Post-Push Hooks

2 min readNov 8, 2025

--

Welcome, friends! 👋
Today, we’ll learn about Git Hooks — what they are, how they work, and how to create your own.

Press enter or click to view image in full size

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

  1. Open your project folder in VS Code or any IDE.
  2. Initialize Git:
git init
  • Open the hidden .git/hooks folder.
  • Rename pre-commit.sample to pre-commit.

Make it executable:

chmod u+x .git/hooks/pre-commit

Edit 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-commit

We’ll dive into this in the next post. Stay tuned!

🗺 Global Git Hooks

To make a hook apply to all repos:

  1. Create a folder:
mkdir -p ~/.git/hooks

Set it as the global hook path:

git config --global core.hooksPath ~/.git/hooks

Add 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

--

--

J3
J3

Written by J3

Computer Engineer | AI & Data Systems | IoT & Embedded Systems | Product Creator — From Idea to Product | Founder @ Jungletronics & Kidstronics

Jungletronics
Jungletronics

Published in Jungletronics

Education & STEAM Learning by building. Practical explorations in AI, programming, electronics, robotics, IoT, and other STEAM topics — from ideas and experiments to working products. Follow and build with us.