Git Pre-commit Hook: Real-World Use Cases
Git hooks are powerful automation features that allow you to run scripts before or after Git events. Among these, the pre-commit hook is especially useful in enforcing standards before changes are committed.
Below is a simple example of a pre-commit hook that blocks committing empty files:
#!/bin/bash
# Loop through all staged files
for file in $(git diff --cached --name-only)
do
# If the file is empty, block the commit
if [ ! -s "$file" ]; then
echo "❌ File '$file' is empty. Please add some content before committing."
exit 1
fi
done
# If all files are valid, allow the commit
exit 0
This is just the beginning! A pre-commit hook can do much more to improve code quality, maintain security, and enforce team practices.
🚀 Where Can You Use Pre-commit Hook? (Real-World Examples)
1. ✅ Code Formatting Check
Ensure that code is properly formatted before committing.
- Python:
black,flake8 - JavaScript:
prettier,eslint
prettier --check .
2. 🔍 Linting (Code Rule Checks)
Prevent syntax errors or bad coding practices from being committed.
eslint . --max-warnings=0
3. 🔐 Secrets Scanner
Detect and prevent committing passwords, tokens, or AWS keys.
detect-secrets scan
4. 📜 Commit Message Format Checker
Enforce a specific commit message format, such as Conventional Commits.
if ! grep -qE "^(feat|fix|docs):" "$1"; then
echo "❌ Commit message must start with feat:, fix:, or docs:"
exit 1
fi
5. 🧪 Run Unit Tests
Automatically run tests before a commit to avoid pushing broken code.
npm test
# or
pytest
6. 📂 Large File Blocker
Block accidentally committed large files (e.g., 50MB+):
for file in $(git diff --cached --name-only); do
if [ $(stat -c%s "$file") -gt 50000000 ]; then
echo "❌ File '$file' is too large!"
exit 1
fi
done
7. 🧹 Auto File Cleanup
Automatically remove trailing spaces or extra newlines:
sed -i 's/[ \t]*$//' "$file" # remove trailing spaces
🧰 Manage Pre-commit Hooks Easily
For complex workflows, use the pre-commit framework. It's a Python-based tool that simplifies hook management.
Example .pre-commit-config.yaml:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
To activate it:
pre-commit install
📦 Summary Table
| Use Case | Benefit |
|---|---|
| Code Format Check | Clean and consistent code |
| Linting | Avoid syntax/style issues |
| Secrets Detection | Prevent leaking credentials |
| Commit Message Format | Enforce team standards |
| Run Tests | Catch bugs early |
| File Size Check | Avoid accidentally committing big files |
| Auto Cleanup | Clean code without manual work |
Final Thoughts
The pre-commit hook is one of the most practical ways to automate checks and enforce policies before a commit is made. By incorporating these hooks, you reduce the risk of introducing errors, maintain consistency, and streamline collaboration across teams.
Add this to your Git workflow today and elevate your development standards!
Keep Learning 🚀
👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.
👉 My YouTube Channel – More videos, more fun, and lots of learning!
👉 📺 Watch this topic in video form
Stay curious, keep coding, and let’s make learning fun together! 🎉